1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/hmdfs/inode_local.c
4 *
5 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
6 */
7
8 #include <linux/file.h>
9 #include <linux/fs_stack.h>
10 #include <linux/kernel.h>
11 #include <linux/mount.h>
12 #include <linux/namei.h>
13 #include <linux/string.h>
14
15 #include "authority/authentication.h"
16 #include "comm/socket_adapter.h"
17 #include "comm/transport.h"
18 #include "hmdfs_client.h"
19 #include "hmdfs_dentryfile.h"
20 #include "hmdfs_device_view.h"
21 #include "hmdfs_share.h"
22 #include "hmdfs_trace.h"
23
24 extern struct kmem_cache *hmdfs_dentry_cachep;
25
26 struct hmdfs_name_data {
27 struct dir_context ctx;
28 const struct qstr *to_find;
29 char *name;
30 bool found;
31 };
32
init_hmdfs_dentry_info(struct hmdfs_sb_info * sbi,struct dentry * dentry,int dentry_type)33 int init_hmdfs_dentry_info(struct hmdfs_sb_info *sbi, struct dentry *dentry,
34 int dentry_type)
35 {
36 struct hmdfs_dentry_info *info =
37 kmem_cache_zalloc(hmdfs_dentry_cachep, GFP_ATOMIC);
38
39 if (!info)
40 return -ENOMEM;
41 dentry->d_fsdata = info;
42 INIT_LIST_HEAD(&info->cache_list_head);
43 INIT_LIST_HEAD(&info->remote_cache_list_head);
44 spin_lock_init(&info->cache_list_lock);
45 mutex_init(&info->remote_cache_list_lock);
46 mutex_init(&info->cache_pull_lock);
47 spin_lock_init(&info->lock);
48 info->dentry_type = dentry_type;
49 info->device_id = 0;
50 if (dentry_type == HMDFS_LAYER_ZERO ||
51 dentry_type == HMDFS_LAYER_FIRST_DEVICE ||
52 dentry_type == HMDFS_LAYER_SECOND_LOCAL ||
53 dentry_type == HMDFS_LAYER_SECOND_REMOTE)
54 d_set_d_op(dentry, &hmdfs_dev_dops);
55 else
56 d_set_d_op(dentry, &hmdfs_dops);
57 return 0;
58 }
59
set_sharefile_flag(struct hmdfs_dentry_info * gdi)60 static inline void set_sharefile_flag(struct hmdfs_dentry_info *gdi)
61 {
62 gdi->file_type = HM_SHARE;
63 }
64
check_and_fixup_share_ops(struct inode * inode,const char * name)65 static void check_and_fixup_share_ops(struct inode *inode,
66 const char *name)
67 {
68 if (is_share_dir(inode, name)) {
69 inode->i_op = &hmdfs_dir_inode_ops_share;
70 inode->i_fop = &hmdfs_dir_ops_share;
71 }
72 }
73
fill_inode_local(struct super_block * sb,struct inode * lower_inode,const char * name)74 struct inode *fill_inode_local(struct super_block *sb,
75 struct inode *lower_inode, const char *name)
76 {
77 int ret = 0;
78 struct inode *inode;
79 struct hmdfs_inode_info *info;
80
81 if (!igrab(lower_inode))
82 return ERR_PTR(-ESTALE);
83
84 inode = hmdfs_iget5_locked_local(sb, lower_inode);
85 if (!inode) {
86 hmdfs_err("iget5_locked get inode NULL");
87 iput(lower_inode);
88 return ERR_PTR(-ENOMEM);
89 }
90 if (!(inode->i_state & I_NEW)) {
91 iput(lower_inode);
92 return inode;
93 }
94
95 info = hmdfs_i(inode);
96 #ifdef CONFIG_HMDFS_FS_PERMISSION
97 info->perm = hmdfs_read_perm(lower_inode);
98 #endif
99 if (S_ISDIR(lower_inode->i_mode))
100 inode->i_mode = (lower_inode->i_mode & S_IFMT) | S_IRWXU |
101 S_IRWXG | S_IXOTH;
102 else if (S_ISREG(lower_inode->i_mode))
103 inode->i_mode = (lower_inode->i_mode & S_IFMT) | S_IRUSR |
104 S_IWUSR | S_IRGRP | S_IWGRP;
105
106 #ifdef CONFIG_HMDFS_FS_PERMISSION
107 inode->i_uid = lower_inode->i_uid;
108 inode->i_gid = lower_inode->i_gid;
109 #else
110 inode->i_uid = KUIDT_INIT((uid_t)1000);
111 inode->i_gid = KGIDT_INIT((gid_t)1000);
112 #endif
113 inode->i_atime = lower_inode->i_atime;
114 inode->i_ctime = lower_inode->i_ctime;
115 inode->i_mtime = lower_inode->i_mtime;
116 inode->i_generation = lower_inode->i_generation;
117
118 info->inode_type = HMDFS_LAYER_OTHER_LOCAL;
119 if (S_ISDIR(lower_inode->i_mode)) {
120 inode->i_op = &hmdfs_dir_inode_ops_local;
121 inode->i_fop = &hmdfs_dir_ops_local;
122 inode->i_mode |= S_IXUGO;
123 } else if (S_ISREG(lower_inode->i_mode)) {
124 inode->i_op = &hmdfs_file_iops_local;
125 inode->i_fop = &hmdfs_file_fops_local;
126 } else {
127 ret = -EIO;
128 goto bad_inode;
129 }
130
131 fsstack_copy_inode_size(inode, lower_inode);
132 check_and_fixup_share_ops(inode, name);
133 unlock_new_inode(inode);
134 return inode;
135 bad_inode:
136 iget_failed(inode);
137 return ERR_PTR(ret);
138 }
139
140 /* hmdfs_convert_lookup_flags - covert hmdfs lookup flags to vfs lookup flags
141 *
142 * @hmdfs_flags: hmdfs lookup flags
143 * @vfs_flags: pointer to converted flags
144 *
145 * return 0 on success, or err code on failure.
146 */
hmdfs_convert_lookup_flags(unsigned int hmdfs_flags,unsigned int * vfs_flags)147 int hmdfs_convert_lookup_flags(unsigned int hmdfs_flags,
148 unsigned int *vfs_flags)
149 {
150 *vfs_flags = 0;
151
152 /* currently only support HMDFS_LOOKUP_REVAL */
153 if (hmdfs_flags & ~HMDFS_LOOKUP_REVAL)
154 return -EINVAL;
155
156 if (hmdfs_flags & HMDFS_LOOKUP_REVAL)
157 *vfs_flags |= LOOKUP_REVAL;
158
159 return 0;
160 }
161
hmdfs_name_match(struct dir_context * ctx,const char * name,int namelen,loff_t offset,u64 ino,unsigned int d_type)162 static int hmdfs_name_match(struct dir_context *ctx, const char *name,
163 int namelen, loff_t offset, u64 ino,
164 unsigned int d_type)
165 {
166 struct hmdfs_name_data *buf =
167 container_of(ctx, struct hmdfs_name_data, ctx);
168 struct qstr candidate = QSTR_INIT(name, namelen);
169
170 if (qstr_case_eq(buf->to_find, &candidate)) {
171 memcpy(buf->name, name, namelen);
172 buf->name[namelen] = 0;
173 buf->found = true;
174 return 1;
175 }
176 return 0;
177 }
178
__lookup_nosensitive(struct path * lower_parent_path,struct dentry * child_dentry,unsigned int flags,struct path * lower_path)179 static int __lookup_nosensitive(struct path *lower_parent_path,
180 struct dentry *child_dentry, unsigned int flags,
181 struct path *lower_path)
182 {
183 struct file *file;
184 const struct cred *cred = current_cred();
185 const struct qstr *name = &child_dentry->d_name;
186 int err;
187 struct hmdfs_name_data buffer = {
188 .ctx.actor = hmdfs_name_match,
189 .to_find = name,
190 .name = __getname(),
191 .found = false,
192 };
193
194 if (!buffer.name) {
195 err = -ENOMEM;
196 goto out;
197 }
198 file = dentry_open(lower_parent_path, O_RDONLY, cred);
199 if (IS_ERR(file)) {
200 err = PTR_ERR(file);
201 goto put_name;
202 }
203 err = iterate_dir(file, &buffer.ctx);
204 fput(file);
205 if (err)
206 goto put_name;
207 if (buffer.found)
208 err = vfs_path_lookup(lower_parent_path->dentry,
209 lower_parent_path->mnt, buffer.name,
210 flags, lower_path);
211 else
212 err = -ENOENT;
213 put_name:
214 __putname(buffer.name);
215 out:
216 return err;
217 }
218
hmdfs_lookup_local(struct inode * parent_inode,struct dentry * child_dentry,unsigned int flags)219 struct dentry *hmdfs_lookup_local(struct inode *parent_inode,
220 struct dentry *child_dentry,
221 unsigned int flags)
222 {
223 const char *d_name = child_dentry->d_name.name;
224 int err = 0;
225 struct path lower_path, lower_parent_path;
226 struct dentry *lower_dentry = NULL, *parent_dentry = NULL, *ret = NULL;
227 struct hmdfs_dentry_info *gdi = NULL;
228 struct inode *child_inode = NULL;
229 struct hmdfs_sb_info *sbi = hmdfs_sb(child_dentry->d_sb);
230
231 trace_hmdfs_lookup_local(parent_inode, child_dentry, flags);
232 if (child_dentry->d_name.len > NAME_MAX) {
233 ret = ERR_PTR(-ENAMETOOLONG);
234 goto out;
235 }
236
237 /* local device */
238 parent_dentry = dget_parent(child_dentry);
239 hmdfs_get_lower_path(parent_dentry, &lower_parent_path);
240 err = init_hmdfs_dentry_info(sbi, child_dentry,
241 HMDFS_LAYER_OTHER_LOCAL);
242 if (err) {
243 ret = ERR_PTR(err);
244 goto out_err;
245 }
246
247 gdi = hmdfs_d(child_dentry);
248
249 flags &= ~LOOKUP_FOLLOW;
250 err = vfs_path_lookup(lower_parent_path.dentry, lower_parent_path.mnt,
251 (child_dentry->d_name.name), 0, &lower_path);
252 if (err == -ENOENT && !sbi->s_case_sensitive)
253 err = __lookup_nosensitive(&lower_parent_path, child_dentry, 0,
254 &lower_path);
255 if (err && err != -ENOENT) {
256 ret = ERR_PTR(err);
257 goto out_err;
258 } else if (!err) {
259 hmdfs_set_lower_path(child_dentry, &lower_path);
260 child_inode = fill_inode_local(parent_inode->i_sb,
261 d_inode(lower_path.dentry),
262 child_dentry->d_name.name);
263
264 if (IS_ERR(child_inode)) {
265 err = PTR_ERR(child_inode);
266 ret = ERR_PTR(err);
267 hmdfs_put_reset_lower_path(child_dentry);
268 goto out_err;
269 }
270 ret = d_splice_alias(child_inode, child_dentry);
271 if (IS_ERR(ret)) {
272 err = PTR_ERR(ret);
273 hmdfs_put_reset_lower_path(child_dentry);
274 goto out_err;
275 }
276
277 check_and_fixup_ownership(parent_inode, child_inode);
278 goto out_err;
279 }
280 /*
281 * return 0 here, so that vfs can continue the process of making this
282 * negative dentry to a positive one while creating a new file.
283 */
284 err = 0;
285 ret = 0;
286
287 lower_dentry = lookup_one_len_unlocked(d_name, lower_parent_path.dentry,
288 child_dentry->d_name.len);
289 if (IS_ERR(lower_dentry)) {
290 err = PTR_ERR(lower_dentry);
291 ret = lower_dentry;
292 goto out_err;
293 }
294 lower_path.dentry = lower_dentry;
295 lower_path.mnt = mntget(lower_parent_path.mnt);
296 hmdfs_set_lower_path(child_dentry, &lower_path);
297
298 out_err:
299 if (!err)
300 hmdfs_set_time(child_dentry, jiffies);
301 hmdfs_put_lower_path(&lower_parent_path);
302 dput(parent_dentry);
303 out:
304 trace_hmdfs_lookup_local_end(parent_inode, child_dentry, err);
305 return ret;
306 }
307
hmdfs_mkdir_local_dentry(struct inode * dir,struct dentry * dentry,umode_t mode)308 int hmdfs_mkdir_local_dentry(struct inode *dir, struct dentry *dentry,
309 umode_t mode)
310 {
311 struct inode *lower_dir = hmdfs_i(dir)->lower_inode;
312 struct dentry *lower_dir_dentry = NULL;
313 struct super_block *sb = dir->i_sb;
314 struct path lower_path;
315 struct dentry *lower_dentry = NULL;
316 int error = 0;
317 struct inode *lower_inode = NULL;
318 struct inode *child_inode = NULL;
319 bool local_res = false;
320 struct cache_fs_override or;
321 __u16 child_perm;
322 kuid_t tmp_uid;
323
324 error = hmdfs_override_dir_id_fs(&or, dir, dentry, &child_perm);
325 if (error)
326 goto cleanup;
327
328 hmdfs_get_lower_path(dentry, &lower_path);
329 lower_dentry = lower_path.dentry;
330 lower_dir_dentry = lock_parent(lower_dentry);
331
332 tmp_uid = hmdfs_override_inode_uid(lower_dir);
333 mode = (mode & S_IFMT) | 00771;
334
335 error = vfs_mkdir(lower_dir, lower_dentry, mode);
336 hmdfs_revert_inode_uid(lower_dir, tmp_uid);
337 if (error) {
338 hmdfs_err("vfs_mkdir() error:%d", error);
339 goto out;
340 }
341 local_res = true;
342 lower_inode = d_inode(lower_dentry);
343 #ifdef CONFIG_HMDFS_FS_PERMISSION
344 error = hmdfs_persist_perm(lower_dentry, &child_perm);
345 #endif
346 child_inode = fill_inode_local(sb, lower_inode, dentry->d_name.name);
347 if (IS_ERR(child_inode)) {
348 error = PTR_ERR(child_inode);
349 goto out;
350 }
351 d_add(dentry, child_inode);
352 set_nlink(dir, hmdfs_i(dir)->lower_inode->i_nlink);
353 out:
354 unlock_dir(lower_dir_dentry);
355 if (local_res)
356 hmdfs_drop_remote_cache_dents(dentry->d_parent);
357
358 if (error) {
359 hmdfs_clear_drop_flag(dentry->d_parent);
360 d_drop(dentry);
361 }
362 hmdfs_put_lower_path(&lower_path);
363 hmdfs_revert_dir_id_fs(&or);
364 cleanup:
365 return error;
366 }
367
hmdfs_mkdir_local(struct inode * dir,struct dentry * dentry,umode_t mode)368 int hmdfs_mkdir_local(struct inode *dir, struct dentry *dentry, umode_t mode)
369 {
370 int err = 0;
371
372 if (check_filename(dentry->d_name.name, dentry->d_name.len)) {
373 err = -EINVAL;
374 return err;
375 }
376
377 if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON) {
378 err = -EACCES;
379 return err;
380 }
381 err = hmdfs_mkdir_local_dentry(dir, dentry, mode);
382 trace_hmdfs_mkdir_local(dir, dentry, err);
383 return err;
384 }
385
hmdfs_create_local_dentry(struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)386 int hmdfs_create_local_dentry(struct inode *dir, struct dentry *dentry,
387 umode_t mode, bool want_excl)
388 {
389 struct inode *lower_dir = NULL;
390 struct dentry *lower_dir_dentry = NULL;
391 struct super_block *sb = dir->i_sb;
392 struct path lower_path;
393 struct dentry *lower_dentry = NULL;
394 int error = 0;
395 struct inode *lower_inode = NULL;
396 struct inode *child_inode = NULL;
397 kuid_t tmp_uid;
398 #ifdef CONFIG_HMDFS_FS_PERMISSION
399 const struct cred *saved_cred = NULL;
400 struct fs_struct *saved_fs = NULL, *copied_fs = NULL;
401 __u16 child_perm;
402 #endif
403
404 #ifdef CONFIG_HMDFS_FS_PERMISSION
405 saved_cred = hmdfs_override_file_fsids(dir, &child_perm);
406 if (!saved_cred) {
407 error = -ENOMEM;
408 goto path_err;
409 }
410
411 saved_fs = current->fs;
412 copied_fs = hmdfs_override_fsstruct(saved_fs);
413 if (!copied_fs) {
414 error = -ENOMEM;
415 goto revert_fsids;
416 }
417 #endif
418 hmdfs_get_lower_path(dentry, &lower_path);
419 lower_dentry = lower_path.dentry;
420 mode = (mode & S_IFMT) | 00660;
421 lower_dir_dentry = lock_parent(lower_dentry);
422 lower_dir = d_inode(lower_dir_dentry);
423 tmp_uid = hmdfs_override_inode_uid(lower_dir);
424 error = vfs_create(lower_dir, lower_dentry, mode, want_excl);
425 hmdfs_revert_inode_uid(lower_dir, tmp_uid);
426 unlock_dir(lower_dir_dentry);
427 if (error)
428 goto out;
429
430 lower_inode = d_inode(lower_dentry);
431 #ifdef CONFIG_HMDFS_FS_PERMISSION
432 error = hmdfs_persist_perm(lower_dentry, &child_perm);
433 #endif
434 child_inode = fill_inode_local(sb, lower_inode, dentry->d_name.name);
435 if (IS_ERR(child_inode)) {
436 error = PTR_ERR(child_inode);
437 goto out_created;
438 }
439 d_add(dentry, child_inode);
440
441 out_created:
442 hmdfs_drop_remote_cache_dents(dentry->d_parent);
443 out:
444 if (error) {
445 hmdfs_clear_drop_flag(dentry->d_parent);
446 d_drop(dentry);
447 }
448 hmdfs_put_lower_path(&lower_path);
449
450 #ifdef CONFIG_HMDFS_FS_PERMISSION
451 hmdfs_revert_fsstruct(saved_fs, copied_fs);
452 revert_fsids:
453 hmdfs_revert_fsids(saved_cred);
454 #endif
455 #ifdef CONFIG_HMDFS_FS_PERMISSION
456 path_err:
457 #endif
458 return error;
459 }
460
hmdfs_create_local(struct inode * dir,struct dentry * child_dentry,umode_t mode,bool want_excl)461 int hmdfs_create_local(struct inode *dir, struct dentry *child_dentry,
462 umode_t mode, bool want_excl)
463 {
464 int err = 0;
465
466 if (check_filename(child_dentry->d_name.name,
467 child_dentry->d_name.len)) {
468 err = -EINVAL;
469 return err;
470 }
471
472 if (hmdfs_file_type(child_dentry->d_name.name) != HMDFS_TYPE_COMMON) {
473 err = -EACCES;
474 return err;
475 }
476
477 err = hmdfs_create_local_dentry(dir, child_dentry, mode, want_excl);
478 trace_hmdfs_create_local(dir, child_dentry, err);
479 return err;
480 }
481
hmdfs_rmdir_local_dentry(struct inode * dir,struct dentry * dentry)482 int hmdfs_rmdir_local_dentry(struct inode *dir, struct dentry *dentry)
483 {
484 struct inode *lower_dir = NULL;
485 struct dentry *lower_dir_dentry = NULL;
486 kuid_t tmp_uid;
487 struct path lower_path;
488 struct dentry *lower_dentry = NULL;
489 int error = 0;
490
491 hmdfs_clear_cache_dents(dentry, true);
492 hmdfs_get_lower_path(dentry, &lower_path);
493 lower_dentry = lower_path.dentry;
494 lower_dir_dentry = lock_parent(lower_dentry);
495 lower_dir = d_inode(lower_dir_dentry);
496 tmp_uid = hmdfs_override_inode_uid(lower_dir);
497
498 error = vfs_rmdir(lower_dir, lower_dentry);
499 hmdfs_revert_inode_uid(lower_dir, tmp_uid);
500 unlock_dir(lower_dir_dentry);
501 hmdfs_put_lower_path(&lower_path);
502 if (error)
503 goto path_err;
504 hmdfs_drop_remote_cache_dents(dentry->d_parent);
505 path_err:
506 if (error)
507 hmdfs_clear_drop_flag(dentry->d_parent);
508 return error;
509 }
510
hmdfs_rmdir_local(struct inode * dir,struct dentry * dentry)511 int hmdfs_rmdir_local(struct inode *dir, struct dentry *dentry)
512 {
513 int err = 0;
514
515 if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON) {
516 err = -EACCES;
517 goto out;
518 }
519
520 err = hmdfs_rmdir_local_dentry(dir, dentry);
521 if (err != 0) {
522 hmdfs_err("rm dir failed:%d", err);
523 goto out;
524 }
525
526 /* drop dentry even remote failed
527 * it maybe cause that one remote devices disconnect
528 * when doing remote rmdir
529 */
530 d_drop(dentry);
531 out:
532 /* return connect device's errcode */
533 trace_hmdfs_rmdir_local(dir, dentry, err);
534 return err;
535 }
536
hmdfs_unlink_local_dentry(struct inode * dir,struct dentry * dentry)537 int hmdfs_unlink_local_dentry(struct inode *dir, struct dentry *dentry)
538 {
539 struct inode *lower_dir = hmdfs_i(dir)->lower_inode;
540 struct dentry *lower_dir_dentry = NULL;
541 struct path lower_path;
542 struct dentry *lower_dentry = NULL;
543 int error;
544 kuid_t tmp_uid;
545
546 hmdfs_get_lower_path(dentry, &lower_path);
547 lower_dentry = lower_path.dentry;
548 dget(lower_dentry);
549 lower_dir_dentry = lock_parent(lower_dentry);
550 tmp_uid = hmdfs_override_inode_uid(lower_dir);
551 error = vfs_unlink(lower_dir, lower_dentry, NULL);
552 hmdfs_revert_inode_uid(lower_dir, tmp_uid);
553 set_nlink(d_inode(dentry),
554 hmdfs_i(d_inode(dentry))->lower_inode->i_nlink);
555 unlock_dir(lower_dir_dentry);
556 dput(lower_dentry);
557 if (error)
558 goto path_err;
559
560 hmdfs_drop_remote_cache_dents(dentry->d_parent);
561 d_drop(dentry);
562 hmdfs_put_lower_path(&lower_path);
563
564 path_err:
565 if (error)
566 hmdfs_clear_drop_flag(dentry->d_parent);
567 return error;
568 }
569
hmdfs_unlink_local(struct inode * dir,struct dentry * dentry)570 int hmdfs_unlink_local(struct inode *dir, struct dentry *dentry)
571 {
572 if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON)
573 return -EACCES;
574
575 return hmdfs_unlink_local_dentry(dir, dentry);
576 }
577
hmdfs_rename_local_dentry(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)578 int hmdfs_rename_local_dentry(struct inode *old_dir, struct dentry *old_dentry,
579 struct inode *new_dir, struct dentry *new_dentry,
580 unsigned int flags)
581 {
582 struct path lower_old_path;
583 struct path lower_new_path;
584 struct dentry *lower_old_dentry = NULL;
585 struct dentry *lower_new_dentry = NULL;
586 struct dentry *lower_old_dir_dentry = NULL;
587 struct dentry *lower_new_dir_dentry = NULL;
588 struct dentry *trap = NULL;
589 int rc = 0;
590 kuid_t old_dir_uid, new_dir_uid;
591
592 if (flags)
593 return -EINVAL;
594
595 hmdfs_get_lower_path(old_dentry, &lower_old_path);
596 lower_old_dentry = lower_old_path.dentry;
597 if (!lower_old_dentry) {
598 hmdfs_err("lower_old_dentry as NULL");
599 rc = -EACCES;
600 goto out_put_old_path;
601 }
602
603 hmdfs_get_lower_path(new_dentry, &lower_new_path);
604 lower_new_dentry = lower_new_path.dentry;
605 if (!lower_new_dentry) {
606 hmdfs_err("lower_new_dentry as NULL");
607 rc = -EACCES;
608 goto out_put_new_path;
609 }
610
611 lower_old_dir_dentry = dget_parent(lower_old_dentry);
612 lower_new_dir_dentry = dget_parent(lower_new_dentry);
613 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
614 old_dir_uid = hmdfs_override_inode_uid(d_inode(lower_old_dir_dentry));
615 new_dir_uid = hmdfs_override_inode_uid(d_inode(lower_new_dir_dentry));
616
617 /* source should not be ancestor of target */
618 if (trap == lower_old_dentry) {
619 rc = -EINVAL;
620 goto out_lock;
621 }
622 /* target should not be ancestor of source */
623 if (trap == lower_new_dentry) {
624 rc = -ENOTEMPTY;
625 goto out_lock;
626 }
627
628 rc = vfs_rename(d_inode(lower_old_dir_dentry), lower_old_dentry,
629 d_inode(lower_new_dir_dentry), lower_new_dentry, NULL,
630 flags);
631 out_lock:
632 dget(old_dentry);
633
634 hmdfs_revert_inode_uid(d_inode(lower_old_dir_dentry), old_dir_uid);
635 hmdfs_revert_inode_uid(d_inode(lower_new_dir_dentry), new_dir_uid);
636
637 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
638 if (rc == 0) {
639 hmdfs_drop_remote_cache_dents(old_dentry->d_parent);
640 if (old_dentry->d_parent != new_dentry->d_parent)
641 hmdfs_drop_remote_cache_dents(new_dentry->d_parent);
642 } else {
643 hmdfs_clear_drop_flag(old_dentry->d_parent);
644 if (old_dentry->d_parent != new_dentry->d_parent)
645 hmdfs_clear_drop_flag(old_dentry->d_parent);
646 d_drop(new_dentry);
647 }
648
649 dput(old_dentry);
650 dput(lower_old_dir_dentry);
651 dput(lower_new_dir_dentry);
652
653 out_put_new_path:
654 hmdfs_put_lower_path(&lower_new_path);
655 out_put_old_path:
656 hmdfs_put_lower_path(&lower_old_path);
657 return rc;
658 }
659
hmdfs_rename_local(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)660 int hmdfs_rename_local(struct inode *old_dir, struct dentry *old_dentry,
661 struct inode *new_dir, struct dentry *new_dentry,
662 unsigned int flags)
663 {
664 int err = 0;
665 int ret = 0;
666
667 trace_hmdfs_rename_local(old_dir, old_dentry, new_dir, new_dentry,
668 flags);
669 if (hmdfs_file_type(old_dentry->d_name.name) != HMDFS_TYPE_COMMON ||
670 hmdfs_file_type(new_dentry->d_name.name) != HMDFS_TYPE_COMMON) {
671 err = -EACCES;
672 goto rename_out;
673 }
674
675 if (S_ISREG(old_dentry->d_inode->i_mode)) {
676 err = hmdfs_rename_local_dentry(old_dir, old_dentry, new_dir,
677 new_dentry, flags);
678 } else if (S_ISDIR(old_dentry->d_inode->i_mode)) {
679 ret = hmdfs_rename_local_dentry(old_dir, old_dentry, new_dir,
680 new_dentry, flags);
681 if (ret != 0) {
682 err = ret;
683 goto rename_out;
684 }
685 }
686
687 if (!err)
688 d_invalidate(old_dentry);
689
690 rename_out:
691 return err;
692 }
693
hmdfs_setattr_local(struct dentry * dentry,struct iattr * ia)694 static int hmdfs_setattr_local(struct dentry *dentry, struct iattr *ia)
695 {
696 struct inode *inode = d_inode(dentry);
697 struct inode *lower_inode = hmdfs_i(inode)->lower_inode;
698 struct path lower_path;
699 struct dentry *lower_dentry = NULL;
700 struct iattr lower_ia;
701 unsigned int ia_valid = ia->ia_valid;
702 int err = 0;
703 kuid_t tmp_uid;
704
705 hmdfs_get_lower_path(dentry, &lower_path);
706 lower_dentry = lower_path.dentry;
707 memcpy(&lower_ia, ia, sizeof(lower_ia));
708 if (ia_valid & ATTR_FILE)
709 lower_ia.ia_file = hmdfs_f(ia->ia_file)->lower_file;
710 lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
711 if (ia_valid & ATTR_SIZE) {
712 err = inode_newsize_ok(inode, ia->ia_size);
713 if (err)
714 goto out;
715 truncate_setsize(inode, ia->ia_size);
716 }
717 inode_lock(lower_inode);
718 tmp_uid = hmdfs_override_inode_uid(lower_inode);
719
720 err = notify_change(lower_dentry, &lower_ia, NULL);
721 i_size_write(inode, i_size_read(lower_inode));
722 inode->i_atime = lower_inode->i_atime;
723 inode->i_mtime = lower_inode->i_mtime;
724 inode->i_ctime = lower_inode->i_ctime;
725 err = update_inode_to_dentry(dentry, inode);
726 hmdfs_revert_inode_uid(lower_inode, tmp_uid);
727
728 inode_unlock(lower_inode);
729 out:
730 hmdfs_put_lower_path(&lower_path);
731 return err;
732 }
733
hmdfs_getattr_local(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)734 static int hmdfs_getattr_local(const struct path *path, struct kstat *stat,
735 u32 request_mask, unsigned int flags)
736 {
737 struct path lower_path;
738 int ret;
739
740 hmdfs_get_lower_path(path->dentry, &lower_path);
741 ret = vfs_getattr(&lower_path, stat, request_mask, flags);
742 stat->ino = d_inode(path->dentry)->i_ino;
743 stat->uid = d_inode(path->dentry)->i_uid;
744 stat->gid = d_inode(path->dentry)->i_gid;
745 hmdfs_put_lower_path(&lower_path);
746
747 return ret;
748 }
749
hmdfs_permission(struct inode * inode,int mask)750 int hmdfs_permission(struct inode *inode, int mask)
751 {
752 #ifdef CONFIG_HMDFS_FS_PERMISSION
753 unsigned int mode = inode->i_mode;
754 kuid_t cur_uid = current_fsuid();
755
756 if (uid_eq(cur_uid, ROOT_UID) || uid_eq(cur_uid, SYSTEM_UID))
757 return 0;
758
759 if (uid_eq(cur_uid, inode->i_uid)) {
760 mode >>= 6;
761 } else if (in_group_p(inode->i_gid)) {
762 mode >>= 3;
763 }
764
765 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
766 return 0;
767
768 trace_hmdfs_permission(inode->i_ino);
769 return -EACCES;
770 #else
771
772 return 0;
773 #endif
774 }
775
hmdfs_local_listxattr(struct dentry * dentry,char * list,size_t size)776 static ssize_t hmdfs_local_listxattr(struct dentry *dentry, char *list,
777 size_t size)
778 {
779 struct path lower_path;
780 ssize_t res = 0;
781 size_t r_size = size;
782
783 if (!hmdfs_support_xattr(dentry))
784 return -EOPNOTSUPP;
785
786 if (size > HMDFS_LISTXATTR_SIZE_MAX)
787 r_size = HMDFS_LISTXATTR_SIZE_MAX;
788
789 hmdfs_get_lower_path(dentry, &lower_path);
790 res = vfs_listxattr(lower_path.dentry, list, r_size);
791 hmdfs_put_lower_path(&lower_path);
792
793 if (res == -ERANGE && r_size != size) {
794 hmdfs_info("no support listxattr size over than %d",
795 HMDFS_LISTXATTR_SIZE_MAX);
796 res = -E2BIG;
797 }
798
799 return res;
800 }
hmdfs_lookup_share(struct inode * parent_inode,struct dentry * child_dentry,unsigned int flags)801 struct dentry *hmdfs_lookup_share(struct inode *parent_inode,
802 struct dentry *child_dentry, unsigned int flags)
803 {
804 const struct qstr *d_name = &child_dentry->d_name;
805 int err = 0;
806 struct dentry *ret = NULL;
807 struct hmdfs_sb_info *sbi = hmdfs_sb(child_dentry->d_sb);
808 struct path src_path;
809 struct inode *child_inode = NULL;
810
811 trace_hmdfs_lookup_share(parent_inode, child_dentry, flags);
812 if (d_name->len > NAME_MAX) {
813 ret = ERR_PTR(-ENAMETOOLONG);
814 goto err_out;
815 }
816
817 err = init_hmdfs_dentry_info(sbi, child_dentry, HMDFS_LAYER_OTHER_LOCAL);
818 if (err) {
819 ret = ERR_PTR(err);
820 goto err_out;
821 }
822
823 err = get_path_from_share_table(sbi, child_dentry, &src_path);
824 if (err) {
825 ret = ERR_PTR(err);
826 goto err_out;
827 }
828
829 hmdfs_set_lower_path(child_dentry, &src_path);
830 child_inode = fill_inode_local(parent_inode->i_sb,
831 d_inode(src_path.dentry), d_name->name);
832
833 set_sharefile_flag(hmdfs_d(child_dentry));
834
835 if (IS_ERR(child_inode)) {
836 err = PTR_ERR(child_inode);
837 ret = ERR_PTR(err);
838 hmdfs_put_reset_lower_path(child_dentry);
839 goto err_out;
840 }
841 ret = d_splice_alias(child_inode, child_dentry);
842 if (IS_ERR(ret)) {
843 err = PTR_ERR(ret);
844 hmdfs_put_reset_lower_path(child_dentry);
845 goto err_out;
846 }
847
848 check_and_fixup_ownership(parent_inode, child_inode);
849
850 err_out:
851 trace_hmdfs_lookup_share_end(parent_inode, child_dentry, err);
852 return ret;
853 }
854
855 const struct inode_operations hmdfs_dir_inode_ops_local = {
856 .lookup = hmdfs_lookup_local,
857 .mkdir = hmdfs_mkdir_local,
858 .create = hmdfs_create_local,
859 .rmdir = hmdfs_rmdir_local,
860 .unlink = hmdfs_unlink_local,
861 .rename = hmdfs_rename_local,
862 .permission = hmdfs_permission,
863 .setattr = hmdfs_setattr_local,
864 .getattr = hmdfs_getattr_local,
865 };
866
867 const struct inode_operations hmdfs_dir_inode_ops_share = {
868 .lookup = hmdfs_lookup_share,
869 .permission = hmdfs_permission,
870 };
871
872 const struct inode_operations hmdfs_file_iops_local = {
873 .setattr = hmdfs_setattr_local,
874 .getattr = hmdfs_getattr_local,
875 .permission = hmdfs_permission,
876 .listxattr = hmdfs_local_listxattr,
877 };
878