• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CLOUD ||
54 	    dentry_type == HMDFS_LAYER_SECOND_REMOTE)
55 		d_set_d_op(dentry, &hmdfs_dev_dops);
56 	else
57 		d_set_d_op(dentry, &hmdfs_dops);
58 	return 0;
59 }
60 
set_sharefile_flag(struct hmdfs_dentry_info * gdi)61 static inline void set_sharefile_flag(struct hmdfs_dentry_info *gdi)
62 {
63 	gdi->file_type = HM_SHARE;
64 }
65 
check_and_fixup_share_ops(struct inode * inode,const char * name)66 static void check_and_fixup_share_ops(struct inode *inode,
67 					const char *name)
68 {
69 	if (is_share_dir(inode, name)) {
70 		inode->i_op = &hmdfs_dir_inode_ops_share;
71 		inode->i_fop = &hmdfs_dir_ops_share;
72 	}
73 }
74 
fill_inode_local(struct super_block * sb,struct inode * lower_inode,const char * name)75 struct inode *fill_inode_local(struct super_block *sb,
76 			       struct inode *lower_inode, const char *name)
77 {
78 	int ret = 0;
79 	struct inode *inode;
80 	struct hmdfs_sb_info *sbi = hmdfs_sb(sb);
81 	struct hmdfs_inode_info *info;
82 
83 	if (!igrab(lower_inode))
84 		return ERR_PTR(-ESTALE);
85 
86 	inode = hmdfs_iget5_locked_local(sb, lower_inode);
87 	if (!inode) {
88 		hmdfs_err("iget5_locked get inode NULL");
89 		iput(lower_inode);
90 		return ERR_PTR(-ENOMEM);
91 	}
92 	if (!(inode->i_state & I_NEW)) {
93 		iput(lower_inode);
94 		return inode;
95 	}
96 
97 	info = hmdfs_i(inode);
98 #ifdef CONFIG_HMDFS_FS_PERMISSION
99 	info->perm = hmdfs_read_perm(lower_inode);
100 #endif
101 	if (S_ISDIR(lower_inode->i_mode))
102 		inode->i_mode = (lower_inode->i_mode & S_IFMT) | S_IRWXU |
103 				S_IRWXG | S_IXOTH;
104 	else if (S_ISREG(lower_inode->i_mode))
105 		inode->i_mode = (lower_inode->i_mode & S_IFMT) | S_IRUSR |
106 				S_IWUSR | S_IRGRP | S_IWGRP;
107 	else if (S_ISLNK(lower_inode->i_mode))
108 		inode->i_mode =
109 			S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
110 
111 #ifdef CONFIG_HMDFS_FS_PERMISSION
112 	inode->i_uid = lower_inode->i_uid;
113 	inode->i_gid = lower_inode->i_gid;
114 #else
115 	inode->i_uid = KUIDT_INIT((uid_t)1000);
116 	inode->i_gid = KGIDT_INIT((gid_t)1000);
117 #endif
118 	inode->i_atime = lower_inode->i_atime;
119 	inode->i_ctime = lower_inode->i_ctime;
120 	inode->i_mtime = lower_inode->i_mtime;
121 	inode->i_generation = lower_inode->i_generation;
122 
123 	info->inode_type = HMDFS_LAYER_OTHER_LOCAL;
124 	if (S_ISDIR(lower_inode->i_mode)) {
125 		inode->i_op = &hmdfs_dir_inode_ops_local;
126 		inode->i_fop = &hmdfs_dir_ops_local;
127 		inode->i_mode |= S_IXUGO;
128 	} else if (S_ISREG(lower_inode->i_mode)) {
129 		inode->i_op = &hmdfs_file_iops_local;
130 		inode->i_fop = &hmdfs_file_fops_local;
131 	} else if (S_ISLNK(lower_inode->i_mode)) {
132 		inode->i_op = &hmdfs_symlink_iops_local;
133 		inode->i_fop = &hmdfs_file_fops_local;
134 		inode->i_size = i_size_read(lower_inode);
135 	} else {
136 		ret = -EIO;
137 		goto bad_inode;
138 	}
139 
140 	if (sbi->s_cloud_disk_switch)
141 		inode->i_mapping->a_ops = &hmdfs_aops_cloud;
142 
143 	fsstack_copy_inode_size(inode, lower_inode);
144 	check_and_fixup_share_ops(inode, name);
145 	unlock_new_inode(inode);
146 	return inode;
147 bad_inode:
148 	iget_failed(inode);
149 	return ERR_PTR(ret);
150 }
151 
152 /* hmdfs_convert_lookup_flags - covert hmdfs lookup flags to vfs lookup flags
153  *
154  * @hmdfs_flags: hmdfs lookup flags
155  * @vfs_flags: pointer to converted flags
156  *
157  * return 0 on success, or err code on failure.
158  */
hmdfs_convert_lookup_flags(unsigned int hmdfs_flags,unsigned int * vfs_flags)159 int hmdfs_convert_lookup_flags(unsigned int hmdfs_flags,
160 			       unsigned int *vfs_flags)
161 {
162 	*vfs_flags = 0;
163 
164 	/* currently only support HMDFS_LOOKUP_REVAL */
165 	if (hmdfs_flags & ~HMDFS_LOOKUP_REVAL)
166 		return -EINVAL;
167 
168 	if (hmdfs_flags & HMDFS_LOOKUP_REVAL)
169 		*vfs_flags |= LOOKUP_REVAL;
170 
171 	return 0;
172 }
173 
hmdfs_name_match(struct dir_context * ctx,const char * name,int namelen,loff_t offset,u64 ino,unsigned int d_type)174 static int hmdfs_name_match(struct dir_context *ctx, const char *name,
175 			    int namelen, loff_t offset, u64 ino,
176 			    unsigned int d_type)
177 {
178 	struct hmdfs_name_data *buf =
179 		container_of(ctx, struct hmdfs_name_data, ctx);
180 	struct qstr candidate = QSTR_INIT(name, namelen);
181 
182 	if (qstr_case_eq(buf->to_find, &candidate)) {
183 		memcpy(buf->name, name, namelen);
184 		buf->name[namelen] = 0;
185 		buf->found = true;
186 		return 1;
187 	}
188 	return 0;
189 }
190 
__lookup_nosensitive(struct path * lower_parent_path,struct dentry * child_dentry,unsigned int flags,struct path * lower_path)191 static int __lookup_nosensitive(struct path *lower_parent_path,
192 				struct dentry *child_dentry, unsigned int flags,
193 				struct path *lower_path)
194 {
195 	struct file *file;
196 	const struct cred *cred = current_cred();
197 	const struct qstr *name = &child_dentry->d_name;
198 	int err;
199 	struct hmdfs_name_data buffer = {
200 		.ctx.actor = hmdfs_name_match,
201 		.to_find = name,
202 		.name = __getname(),
203 		.found = false,
204 	};
205 
206 	if (!buffer.name) {
207 		err = -ENOMEM;
208 		goto out;
209 	}
210 	file = dentry_open(lower_parent_path, O_RDONLY, cred);
211 	if (IS_ERR(file)) {
212 		err = PTR_ERR(file);
213 		goto put_name;
214 	}
215 	err = iterate_dir(file, &buffer.ctx);
216 	fput(file);
217 	if (err)
218 		goto put_name;
219 	if (buffer.found)
220 		err = vfs_path_lookup(lower_parent_path->dentry,
221 				      lower_parent_path->mnt, buffer.name,
222 				      flags, lower_path);
223 	else
224 		err = -ENOENT;
225 put_name:
226 	__putname(buffer.name);
227 out:
228 	return err;
229 }
230 
set_symlink_flag(struct hmdfs_dentry_info * gdi)231 static inline void set_symlink_flag(struct hmdfs_dentry_info *gdi)
232 {
233 	gdi->file_type = HM_SYMLINK;
234 }
235 
hmdfs_lookup_local(struct inode * parent_inode,struct dentry * child_dentry,unsigned int flags)236 struct dentry *hmdfs_lookup_local(struct inode *parent_inode,
237 				  struct dentry *child_dentry,
238 				  unsigned int flags)
239 {
240 	const char *d_name = child_dentry->d_name.name;
241 	int err = 0;
242 	struct path lower_path, lower_parent_path;
243 	struct dentry *lower_dentry = NULL, *parent_dentry = NULL, *ret = NULL;
244 	struct hmdfs_dentry_info *gdi = NULL;
245 	struct inode *child_inode = NULL;
246 	struct hmdfs_sb_info *sbi = hmdfs_sb(child_dentry->d_sb);
247 
248 	trace_hmdfs_lookup_local(parent_inode, child_dentry, flags);
249 	if (child_dentry->d_name.len > NAME_MAX) {
250 		ret = ERR_PTR(-ENAMETOOLONG);
251 		goto out;
252 	}
253 
254 	/* local device */
255 	parent_dentry = dget_parent(child_dentry);
256 	hmdfs_get_lower_path(parent_dentry, &lower_parent_path);
257 	err = init_hmdfs_dentry_info(sbi, child_dentry,
258 				     HMDFS_LAYER_OTHER_LOCAL);
259 	if (err) {
260 		ret = ERR_PTR(err);
261 		goto out_err;
262 	}
263 
264 	gdi = hmdfs_d(child_dentry);
265 
266 	flags &= ~LOOKUP_FOLLOW;
267 	err = vfs_path_lookup(lower_parent_path.dentry, lower_parent_path.mnt,
268 			      (child_dentry->d_name.name), 0, &lower_path);
269 	if (err == -ENOENT && !sbi->s_case_sensitive)
270 		err = __lookup_nosensitive(&lower_parent_path, child_dentry, 0,
271 					   &lower_path);
272 	if (err && err != -ENOENT) {
273 		ret = ERR_PTR(err);
274 		goto out_err;
275 	} else if (!err) {
276 		hmdfs_set_lower_path(child_dentry, &lower_path);
277 		child_inode = fill_inode_local(parent_inode->i_sb,
278 					       d_inode(lower_path.dentry),
279 						   child_dentry->d_name.name);
280 
281 		if (S_ISLNK(d_inode(lower_path.dentry)->i_mode))
282 			set_symlink_flag(gdi);
283 		if (IS_ERR(child_inode)) {
284 			err = PTR_ERR(child_inode);
285 			ret = ERR_PTR(err);
286 			hmdfs_put_reset_lower_path(child_dentry);
287 			goto out_err;
288 		}
289 		ret = d_splice_alias(child_inode, child_dentry);
290 		if (IS_ERR(ret)) {
291 			err = PTR_ERR(ret);
292 			hmdfs_put_reset_lower_path(child_dentry);
293 			goto out_err;
294 		}
295 
296 		check_and_fixup_ownership(parent_inode, child_inode);
297 		goto out_err;
298 	}
299 	/*
300 	 * return 0 here, so that vfs can continue the process of making this
301 	 * negative dentry to a positive one while creating a new file.
302 	 */
303 	err = 0;
304 	ret = 0;
305 
306 	lower_dentry = lookup_one_len_unlocked(d_name, lower_parent_path.dentry,
307 					       child_dentry->d_name.len);
308 	if (IS_ERR(lower_dentry)) {
309 		err = PTR_ERR(lower_dentry);
310 		ret = lower_dentry;
311 		goto out_err;
312 	}
313 	lower_path.dentry = lower_dentry;
314 	lower_path.mnt = mntget(lower_parent_path.mnt);
315 	hmdfs_set_lower_path(child_dentry, &lower_path);
316 
317 out_err:
318 	if (!err)
319 		hmdfs_set_time(child_dentry, jiffies);
320 	hmdfs_put_lower_path(&lower_parent_path);
321 	dput(parent_dentry);
322 out:
323 	trace_hmdfs_lookup_local_end(parent_inode, child_dentry, err);
324 	return ret;
325 }
326 
hmdfs_mkdir_local_dentry(struct inode * dir,struct dentry * dentry,umode_t mode)327 int hmdfs_mkdir_local_dentry(struct inode *dir, struct dentry *dentry,
328 			     umode_t mode)
329 {
330 	struct inode *lower_dir = hmdfs_i(dir)->lower_inode;
331 	struct dentry *lower_dir_dentry = NULL;
332 	struct super_block *sb = dir->i_sb;
333 	struct path lower_path;
334 	struct dentry *lower_dentry = NULL;
335 	int error = 0;
336 	struct inode *lower_inode = NULL;
337 	struct inode *child_inode = NULL;
338 	bool local_res = false;
339 	struct cache_fs_override or;
340 	__u16 child_perm;
341 	kuid_t tmp_uid;
342 
343 	error = hmdfs_override_dir_id_fs(&or, dir, dentry, &child_perm);
344 	if (error)
345 		goto cleanup;
346 
347 	hmdfs_get_lower_path(dentry, &lower_path);
348 	lower_dentry = lower_path.dentry;
349 	lower_dir_dentry = lock_parent(lower_dentry);
350 
351 	tmp_uid = hmdfs_override_inode_uid(lower_dir);
352 	mode = (mode & S_IFMT) | 00771;
353 
354 	error = vfs_mkdir(lower_dir, lower_dentry, mode);
355 	hmdfs_revert_inode_uid(lower_dir, tmp_uid);
356 	if (error) {
357 		hmdfs_err("vfs_mkdir() error:%d", error);
358 		goto out;
359 	}
360 	local_res = true;
361 	lower_inode = d_inode(lower_dentry);
362 #ifdef CONFIG_HMDFS_FS_PERMISSION
363 	error = hmdfs_persist_perm(lower_dentry, &child_perm);
364 #endif
365 	child_inode = fill_inode_local(sb, lower_inode, dentry->d_name.name);
366 	if (IS_ERR(child_inode)) {
367 		error = PTR_ERR(child_inode);
368 		goto out;
369 	}
370 	d_add(dentry, child_inode);
371 	set_nlink(dir, hmdfs_i(dir)->lower_inode->i_nlink);
372 out:
373 	unlock_dir(lower_dir_dentry);
374 	if (local_res)
375 		hmdfs_drop_remote_cache_dents(dentry->d_parent);
376 
377 	if (error) {
378 		hmdfs_clear_drop_flag(dentry->d_parent);
379 		d_drop(dentry);
380 	}
381 	hmdfs_put_lower_path(&lower_path);
382 	hmdfs_revert_dir_id_fs(&or);
383 cleanup:
384 	return error;
385 }
386 
hmdfs_mkdir_local(struct inode * dir,struct dentry * dentry,umode_t mode)387 int hmdfs_mkdir_local(struct inode *dir, struct dentry *dentry, umode_t mode)
388 {
389 	int err = 0;
390 
391 	if (check_filename(dentry->d_name.name, dentry->d_name.len)) {
392 		err = -EINVAL;
393 		return err;
394 	}
395 
396 	if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON) {
397 		err = -EACCES;
398 		return err;
399 	}
400 	err = hmdfs_mkdir_local_dentry(dir, dentry, mode);
401 	trace_hmdfs_mkdir_local(dir, dentry, err);
402 	return err;
403 }
404 
hmdfs_create_local_dentry(struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)405 int hmdfs_create_local_dentry(struct inode *dir, struct dentry *dentry,
406 			      umode_t mode, bool want_excl)
407 {
408 	struct inode *lower_dir = NULL;
409 	struct dentry *lower_dir_dentry = NULL;
410 	struct super_block *sb = dir->i_sb;
411 	struct path lower_path;
412 	struct dentry *lower_dentry = NULL;
413 	int error = 0;
414 	struct inode *lower_inode = NULL;
415 	struct inode *child_inode = NULL;
416 	kuid_t tmp_uid;
417 #ifdef CONFIG_HMDFS_FS_PERMISSION
418 	const struct cred *saved_cred = NULL;
419 	struct fs_struct *saved_fs = NULL, *copied_fs = NULL;
420 	__u16 child_perm;
421 #endif
422 
423 #ifdef CONFIG_HMDFS_FS_PERMISSION
424 	saved_cred = hmdfs_override_file_fsids(dir, &child_perm);
425 	if (!saved_cred) {
426 		error = -ENOMEM;
427 		goto path_err;
428 	}
429 
430 	saved_fs = current->fs;
431 	copied_fs = hmdfs_override_fsstruct(saved_fs);
432 	if (!copied_fs) {
433 		error = -ENOMEM;
434 		goto revert_fsids;
435 	}
436 #endif
437 	hmdfs_get_lower_path(dentry, &lower_path);
438 	lower_dentry = lower_path.dentry;
439 	mode = (mode & S_IFMT) | 00660;
440 	lower_dir_dentry = lock_parent(lower_dentry);
441 	lower_dir = d_inode(lower_dir_dentry);
442 	tmp_uid = hmdfs_override_inode_uid(lower_dir);
443 	error = vfs_create(lower_dir, lower_dentry, mode, want_excl);
444 	hmdfs_revert_inode_uid(lower_dir, tmp_uid);
445 	unlock_dir(lower_dir_dentry);
446 	if (error)
447 		goto out;
448 
449 	lower_inode = d_inode(lower_dentry);
450 #ifdef CONFIG_HMDFS_FS_PERMISSION
451 	error = hmdfs_persist_perm(lower_dentry, &child_perm);
452 #endif
453 	child_inode = fill_inode_local(sb, lower_inode, dentry->d_name.name);
454 	if (IS_ERR(child_inode)) {
455 		error = PTR_ERR(child_inode);
456 		goto out_created;
457 	}
458 	d_add(dentry, child_inode);
459 
460 out_created:
461 	hmdfs_drop_remote_cache_dents(dentry->d_parent);
462 out:
463 	if (error) {
464 		hmdfs_clear_drop_flag(dentry->d_parent);
465 		d_drop(dentry);
466 	}
467 	hmdfs_put_lower_path(&lower_path);
468 
469 #ifdef CONFIG_HMDFS_FS_PERMISSION
470 	hmdfs_revert_fsstruct(saved_fs, copied_fs);
471 revert_fsids:
472 	hmdfs_revert_fsids(saved_cred);
473 #endif
474 #ifdef CONFIG_HMDFS_FS_PERMISSION
475 path_err:
476 #endif
477 	return error;
478 }
479 
hmdfs_create_local(struct inode * dir,struct dentry * child_dentry,umode_t mode,bool want_excl)480 int hmdfs_create_local(struct inode *dir, struct dentry *child_dentry,
481 		       umode_t mode, bool want_excl)
482 {
483 	int err = 0;
484 
485 	if (check_filename(child_dentry->d_name.name,
486 			   child_dentry->d_name.len)) {
487 		err = -EINVAL;
488 		return err;
489 	}
490 
491 	if (hmdfs_file_type(child_dentry->d_name.name) != HMDFS_TYPE_COMMON) {
492 		err = -EACCES;
493 		return err;
494 	}
495 
496 	err = hmdfs_create_local_dentry(dir, child_dentry, mode, want_excl);
497 	trace_hmdfs_create_local(dir, child_dentry, err);
498 	return err;
499 }
500 
hmdfs_rmdir_local_dentry(struct inode * dir,struct dentry * dentry)501 int hmdfs_rmdir_local_dentry(struct inode *dir, struct dentry *dentry)
502 {
503 	struct inode *lower_dir = NULL;
504 	struct dentry *lower_dir_dentry = NULL;
505 	kuid_t tmp_uid;
506 	struct path lower_path;
507 	struct dentry *lower_dentry = NULL;
508 	struct dentry *lookup_dentry = NULL;
509 	int error = 0;
510 
511 	hmdfs_clear_cache_dents(dentry, true);
512 	hmdfs_get_lower_path(dentry, &lower_path);
513 	lower_dentry = lower_path.dentry;
514 	lower_dir_dentry = lock_parent(lower_dentry);
515 	lower_dir = d_inode(lower_dir_dentry);
516 
517 	lookup_dentry = lookup_one_len(lower_dentry->d_name.name, lower_dir_dentry,
518 				       lower_dentry->d_name.len);
519 	if (IS_ERR(lookup_dentry)) {
520 		error = PTR_ERR(lookup_dentry);
521 		hmdfs_err("lookup_one_len failed, err = %d", error);
522 		goto lookup_err;
523 	}
524 	tmp_uid = hmdfs_override_inode_uid(lower_dir);
525 
526 	error = vfs_rmdir(lower_dir, lookup_dentry);
527 	hmdfs_revert_inode_uid(lower_dir, tmp_uid);
528 	dput(lookup_dentry);
529 lookup_err:
530 	unlock_dir(lower_dir_dentry);
531 	hmdfs_put_lower_path(&lower_path);
532 	if (error)
533 		goto path_err;
534 	hmdfs_drop_remote_cache_dents(dentry->d_parent);
535 path_err:
536 	if (error)
537 		hmdfs_clear_drop_flag(dentry->d_parent);
538 	return error;
539 }
540 
hmdfs_rmdir_local(struct inode * dir,struct dentry * dentry)541 int hmdfs_rmdir_local(struct inode *dir, struct dentry *dentry)
542 {
543 	int err = 0;
544 
545 	if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON) {
546 		err = -EACCES;
547 		goto out;
548 	}
549 
550 	err = hmdfs_rmdir_local_dentry(dir, dentry);
551 	if (err != 0) {
552 		hmdfs_err("rm dir failed:%d", err);
553 		goto out;
554 	}
555 
556 	/* drop dentry even remote failed
557 	 * it maybe cause that one remote devices disconnect
558 	 * when doing remote rmdir
559 	 */
560 	d_drop(dentry);
561 out:
562 	/* return connect device's errcode */
563 	trace_hmdfs_rmdir_local(dir, dentry, err);
564 	return err;
565 }
566 
hmdfs_unlink_local_dentry(struct inode * dir,struct dentry * dentry)567 int hmdfs_unlink_local_dentry(struct inode *dir, struct dentry *dentry)
568 {
569 	struct dentry *lower_dir_dentry = NULL;
570 	struct path lower_path;
571 	struct inode *lower_dir = NULL;
572 	struct dentry *lower_dentry = NULL;
573 	struct dentry *lookup_dentry = NULL;
574 	int error;
575 	kuid_t tmp_uid;
576 
577 	hmdfs_get_lower_path(dentry, &lower_path);
578 	lower_dentry = lower_path.dentry;
579 	dget(lower_dentry);
580 	lower_dir_dentry = lock_parent(lower_dentry);
581 	lower_dir = d_inode(lower_dir_dentry);
582 	lookup_dentry = lookup_one_len(lower_dentry->d_name.name, lower_dir_dentry,
583 				       lower_dentry->d_name.len);
584 	if (IS_ERR(lookup_dentry)) {
585 		error = PTR_ERR(lookup_dentry);
586 		hmdfs_err("lookup_one_len failed, err = %d", error);
587 		goto lookup_err;
588 	}
589 
590 	tmp_uid = hmdfs_override_inode_uid(lower_dir);
591 	error = vfs_unlink(lower_dir, lookup_dentry, NULL);
592 	hmdfs_revert_inode_uid(lower_dir, tmp_uid);
593 	set_nlink(d_inode(dentry),
594 		  hmdfs_i(d_inode(dentry))->lower_inode->i_nlink);
595 	dput(lookup_dentry);
596 lookup_err:
597 	unlock_dir(lower_dir_dentry);
598 	dput(lower_dentry);
599 	if (error)
600 		goto path_err;
601 
602 	hmdfs_drop_remote_cache_dents(dentry->d_parent);
603 	d_drop(dentry);
604 	hmdfs_put_lower_path(&lower_path);
605 
606 path_err:
607 	if (error)
608 		hmdfs_clear_drop_flag(dentry->d_parent);
609 	return error;
610 }
611 
hmdfs_unlink_local(struct inode * dir,struct dentry * dentry)612 int hmdfs_unlink_local(struct inode *dir, struct dentry *dentry)
613 {
614 	if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON)
615 		return -EACCES;
616 
617 	return hmdfs_unlink_local_dentry(dir, dentry);
618 }
619 
hmdfs_rename_local_dentry(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)620 int hmdfs_rename_local_dentry(struct inode *old_dir, struct dentry *old_dentry,
621 			      struct inode *new_dir, struct dentry *new_dentry,
622 			      unsigned int flags)
623 {
624 	struct path lower_old_path;
625 	struct path lower_new_path;
626 	struct dentry *lower_old_dentry = NULL;
627 	struct dentry *lower_new_dentry = NULL;
628 	struct dentry *lower_old_dir_dentry = NULL;
629 	struct dentry *lower_new_dir_dentry = NULL;
630 	struct dentry *trap = NULL;
631 	int rc = 0;
632 	kuid_t old_dir_uid, new_dir_uid;
633 
634 	if (flags)
635 		return -EINVAL;
636 
637 	hmdfs_get_lower_path(old_dentry, &lower_old_path);
638 	lower_old_dentry = lower_old_path.dentry;
639 	if (!lower_old_dentry) {
640 		hmdfs_err("lower_old_dentry as NULL");
641 		rc = -EACCES;
642 		goto out_put_old_path;
643 	}
644 
645 	hmdfs_get_lower_path(new_dentry, &lower_new_path);
646 	lower_new_dentry = lower_new_path.dentry;
647 	if (!lower_new_dentry) {
648 		hmdfs_err("lower_new_dentry as NULL");
649 		rc = -EACCES;
650 		goto out_put_new_path;
651 	}
652 
653 	lower_old_dir_dentry = dget_parent(lower_old_dentry);
654 	lower_new_dir_dentry = dget_parent(lower_new_dentry);
655 	trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
656 	new_dir_uid = hmdfs_override_inode_uid(d_inode(lower_new_dir_dentry));
657 	old_dir_uid = hmdfs_override_inode_uid(d_inode(lower_old_dir_dentry));
658 
659 	/* source should not be ancestor of target */
660 	if (trap == lower_old_dentry) {
661 		rc = -EINVAL;
662 		goto out_lock;
663 	}
664 	/* target should not be ancestor of source */
665 	if (trap == lower_new_dentry) {
666 		rc = -ENOTEMPTY;
667 		goto out_lock;
668 	}
669 
670 	rc = vfs_rename(d_inode(lower_old_dir_dentry), lower_old_dentry,
671 			d_inode(lower_new_dir_dentry), lower_new_dentry, NULL,
672 			flags);
673 out_lock:
674 	dget(old_dentry);
675 
676 	hmdfs_revert_inode_uid(d_inode(lower_old_dir_dentry), old_dir_uid);
677 	hmdfs_revert_inode_uid(d_inode(lower_new_dir_dentry), new_dir_uid);
678 
679 	unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
680 	if (rc == 0) {
681 		hmdfs_drop_remote_cache_dents(old_dentry->d_parent);
682 		if (old_dentry->d_parent != new_dentry->d_parent)
683 			hmdfs_drop_remote_cache_dents(new_dentry->d_parent);
684 	} else {
685 		hmdfs_clear_drop_flag(old_dentry->d_parent);
686 		if (old_dentry->d_parent != new_dentry->d_parent)
687 			hmdfs_clear_drop_flag(old_dentry->d_parent);
688 		d_drop(new_dentry);
689 	}
690 
691 	dput(old_dentry);
692 	dput(lower_old_dir_dentry);
693 	dput(lower_new_dir_dentry);
694 
695 out_put_new_path:
696 	hmdfs_put_lower_path(&lower_new_path);
697 out_put_old_path:
698 	hmdfs_put_lower_path(&lower_old_path);
699 	return rc;
700 }
701 
hmdfs_rename_local(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)702 int hmdfs_rename_local(struct inode *old_dir, struct dentry *old_dentry,
703 		       struct inode *new_dir, struct dentry *new_dentry,
704 		       unsigned int flags)
705 {
706 	int err = 0;
707 	int ret = 0;
708 
709 	trace_hmdfs_rename_local(old_dir, old_dentry, new_dir, new_dentry,
710 				 flags);
711 	if (hmdfs_file_type(old_dentry->d_name.name) != HMDFS_TYPE_COMMON ||
712 	    hmdfs_file_type(new_dentry->d_name.name) != HMDFS_TYPE_COMMON) {
713 		err = -EACCES;
714 		goto rename_out;
715 	}
716 
717 	if (hmdfs_i(old_dir)->inode_type != hmdfs_i(new_dir)->inode_type) {
718 		hmdfs_err("in different view");
719 		err = -EPERM;
720 		goto rename_out;
721 	}
722 
723 	if (hmdfs_d(old_dentry)->device_id != hmdfs_d(new_dentry)->device_id) {
724 		err = -EXDEV;
725 		goto rename_out;
726 	}
727 
728 	if (S_ISREG(old_dentry->d_inode->i_mode)) {
729 		err = hmdfs_rename_local_dentry(old_dir, old_dentry, new_dir,
730 						new_dentry, flags);
731 	} else if (S_ISDIR(old_dentry->d_inode->i_mode)) {
732 		ret = hmdfs_rename_local_dentry(old_dir, old_dentry, new_dir,
733 						new_dentry, flags);
734 		if (ret != 0) {
735 			err = ret;
736 			goto rename_out;
737 		}
738 	}
739 
740 	if (!err)
741 		d_invalidate(old_dentry);
742 
743 rename_out:
744 	return err;
745 }
746 
symname_is_allowed(const char * symname)747 static bool symname_is_allowed(const char *symname)
748 {
749 	char *p;
750 	char *buf = 0;
751 	size_t symname_len;
752 
753 	symname_len = strnlen(symname, PATH_MAX);
754 	if (symname_len >= PATH_MAX)
755 		return false;
756 
757 	buf = kzalloc(PATH_MAX + 2, GFP_KERNEL);
758 	if (!buf)
759 		return false;
760 
761 	buf[0] = '/';
762 	strncpy(buf + 1, symname, symname_len);
763 	strcat(buf, "/");
764 	p = strstr(buf, "/../");
765 	if (p) {
766 		kfree(buf);
767 		return false;
768 	}
769 
770 	kfree(buf);
771 	return true;
772 }
773 
hmdfs_symlink_local(struct inode * dir,struct dentry * dentry,const char * symname)774 int hmdfs_symlink_local(struct inode *dir, struct dentry *dentry,
775 			const char *symname)
776 {
777 	int err;
778 	struct dentry *lower_dentry = NULL;
779 	struct dentry *lower_parent_dentry = NULL;
780 	struct path lower_path;
781 	struct inode *child_inode = NULL;
782 	struct inode *lower_dir_inode = hmdfs_i(dir)->lower_inode;
783 	struct hmdfs_dentry_info *gdi = hmdfs_d(dentry);
784 	kuid_t tmp_uid;
785 #ifdef CONFIG_HMDFS_FS_PERMISSION
786 	const struct cred *saved_cred = NULL;
787 	struct fs_struct *saved_fs = NULL, *copied_fs = NULL;
788 	__u16 child_perm;
789 #endif
790 
791 	if (unlikely(!symname_is_allowed(symname))) {
792 		err = -EPERM;
793 		goto path_err;
794 	}
795 
796 #ifdef CONFIG_HMDFS_FS_PERMISSION
797 	saved_cred = hmdfs_override_file_fsids(dir, &child_perm);
798 	if (!saved_cred) {
799 		err = -ENOMEM;
800 		goto path_err;
801 	}
802 
803 	saved_fs = current->fs;
804 	copied_fs = hmdfs_override_fsstruct(saved_fs);
805 	if (!copied_fs) {
806 		err = -ENOMEM;
807 		goto revert_fsids;
808 	}
809 #endif
810 	hmdfs_get_lower_path(dentry, &lower_path);
811 	lower_dentry = lower_path.dentry;
812 	lower_parent_dentry = lock_parent(lower_dentry);
813 	tmp_uid = hmdfs_override_inode_uid(lower_dir_inode);
814 	err = vfs_symlink(lower_dir_inode, lower_dentry, symname);
815 	hmdfs_revert_inode_uid(lower_dir_inode, tmp_uid);
816 	unlock_dir(lower_parent_dentry);
817 	if (err)
818 		goto out_err;
819 	set_symlink_flag(gdi);
820 #ifdef CONFIG_HMDFS_FS_PERMISSION
821 	err = hmdfs_persist_perm(lower_dentry, &child_perm);
822 #endif
823 	child_inode = fill_inode_local(dir->i_sb, d_inode(lower_dentry),
824 				       dentry->d_name.name);
825 	if (IS_ERR(child_inode)) {
826 		err = PTR_ERR(child_inode);
827 		goto out_err;
828 	}
829 	d_add(dentry, child_inode);
830 	fsstack_copy_attr_times(dir, lower_dir_inode);
831 	fsstack_copy_inode_size(dir, lower_dir_inode);
832 
833 out_err:
834 	hmdfs_put_lower_path(&lower_path);
835 #ifdef CONFIG_HMDFS_FS_PERMISSION
836 	hmdfs_revert_fsstruct(saved_fs, copied_fs);
837 revert_fsids:
838 	hmdfs_revert_fsids(saved_cred);
839 #endif
840 path_err:
841 	return err;
842 }
843 
hmdfs_get_link_local(struct dentry * dentry,struct inode * inode,struct delayed_call * done)844 static const char *hmdfs_get_link_local(struct dentry *dentry,
845 					struct inode *inode,
846 					struct delayed_call *done)
847 {
848 	const char *link = NULL;
849 	struct dentry *lower_dentry = NULL;
850 	struct inode *lower_inode = NULL;
851 	struct path lower_path;
852 
853 	if(!dentry) {
854 		hmdfs_err("dentry MULL");
855 		link = ERR_PTR(-ECHILD);
856 		goto link_out;
857 	}
858 
859 	hmdfs_get_lower_path(dentry, &lower_path);
860 	lower_dentry = lower_path.dentry;
861 	lower_inode = d_inode(lower_dentry);
862 	if(!lower_inode->i_op || !lower_inode->i_op->get_link) {
863 		hmdfs_err("The lower inode doesn't support get_link i_op");
864 		link = ERR_PTR(-EINVAL);
865 		goto out;
866 	}
867 
868 	link = lower_inode->i_op->get_link(lower_dentry, lower_inode, done);
869 	if(IS_ERR_OR_NULL(link))
870 		goto out;
871 	fsstack_copy_attr_atime(inode, lower_inode);
872 out:
873 	hmdfs_put_lower_path(&lower_path);
874 link_out:
875 	return link;
876 }
877 
hmdfs_setattr_local(struct dentry * dentry,struct iattr * ia)878 static int hmdfs_setattr_local(struct dentry *dentry, struct iattr *ia)
879 {
880 	struct inode *inode = d_inode(dentry);
881 	struct inode *lower_inode = hmdfs_i(inode)->lower_inode;
882 	struct path lower_path;
883 	struct dentry *lower_dentry = NULL;
884 	struct iattr lower_ia;
885 	unsigned int ia_valid = ia->ia_valid;
886 	int err = 0;
887 	kuid_t tmp_uid;
888 
889 	hmdfs_get_lower_path(dentry, &lower_path);
890 	lower_dentry = lower_path.dentry;
891 	memcpy(&lower_ia, ia, sizeof(lower_ia));
892 	if (ia_valid & ATTR_FILE)
893 		lower_ia.ia_file = hmdfs_f(ia->ia_file)->lower_file;
894 	lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
895 	if (ia_valid & ATTR_SIZE) {
896 		err = inode_newsize_ok(inode, ia->ia_size);
897 		if (err)
898 			goto out;
899 		truncate_setsize(inode, ia->ia_size);
900 	}
901 	inode_lock(lower_inode);
902 	tmp_uid = hmdfs_override_inode_uid(lower_inode);
903 
904 	err = notify_change(lower_dentry, &lower_ia, NULL);
905 	i_size_write(inode, i_size_read(lower_inode));
906 	inode->i_atime = lower_inode->i_atime;
907 	inode->i_mtime = lower_inode->i_mtime;
908 	inode->i_ctime = lower_inode->i_ctime;
909 	err = update_inode_to_dentry(dentry, inode);
910 	hmdfs_revert_inode_uid(lower_inode, tmp_uid);
911 
912 	inode_unlock(lower_inode);
913 out:
914 	hmdfs_put_lower_path(&lower_path);
915 	return err;
916 }
917 
hmdfs_getattr_local(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)918 static int hmdfs_getattr_local(const struct path *path, struct kstat *stat,
919 			       u32 request_mask, unsigned int flags)
920 {
921 	struct path lower_path;
922 	int ret;
923 
924 	hmdfs_get_lower_path(path->dentry, &lower_path);
925 	ret = vfs_getattr(&lower_path, stat, request_mask, flags);
926 	stat->ino = d_inode(path->dentry)->i_ino;
927 	stat->uid = d_inode(path->dentry)->i_uid;
928 	stat->gid = d_inode(path->dentry)->i_gid;
929 	stat->dev = 0;
930 	stat->rdev = 0;
931 	hmdfs_put_lower_path(&lower_path);
932 
933 	return ret;
934 }
935 
hmdfs_permission(struct inode * inode,int mask)936 int hmdfs_permission(struct inode *inode, int mask)
937 {
938 #ifdef CONFIG_HMDFS_FS_PERMISSION
939 	unsigned int mode = inode->i_mode;
940 	kuid_t cur_uid = current_fsuid();
941 
942 	if (uid_eq(cur_uid, ROOT_UID) || uid_eq(cur_uid, SYSTEM_UID))
943 		return 0;
944 
945 	if (uid_eq(cur_uid, inode->i_uid)) {
946 		mode >>= 6;
947 	} else if (in_group_p(inode->i_gid)) {
948 		mode >>= 3;
949 	}
950 
951 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
952 		return 0;
953 
954 	trace_hmdfs_permission(inode->i_ino);
955 	return -EACCES;
956 #else
957 
958 	return 0;
959 #endif
960 }
961 
hmdfs_local_listxattr(struct dentry * dentry,char * list,size_t size)962 static ssize_t hmdfs_local_listxattr(struct dentry *dentry, char *list,
963 				     size_t size)
964 {
965 	struct path lower_path;
966 	ssize_t res = 0;
967 	size_t r_size = size;
968 
969 	if (!hmdfs_support_xattr(dentry))
970 		return -EOPNOTSUPP;
971 
972 	if (size > HMDFS_LISTXATTR_SIZE_MAX)
973 		r_size = HMDFS_LISTXATTR_SIZE_MAX;
974 
975 	hmdfs_get_lower_path(dentry, &lower_path);
976 	res = vfs_listxattr(lower_path.dentry, list, r_size);
977 	hmdfs_put_lower_path(&lower_path);
978 
979 	if (res == -ERANGE && r_size != size) {
980 		hmdfs_info("no support listxattr size over than %d",
981 			   HMDFS_LISTXATTR_SIZE_MAX);
982 		res = -E2BIG;
983 	}
984 
985 	return res;
986 }
hmdfs_lookup_share(struct inode * parent_inode,struct dentry * child_dentry,unsigned int flags)987 struct dentry *hmdfs_lookup_share(struct inode *parent_inode,
988 				struct dentry *child_dentry, unsigned int flags)
989 {
990 	const struct qstr *d_name = &child_dentry->d_name;
991 	int err = 0;
992 	struct dentry *ret = NULL;
993 	struct hmdfs_sb_info *sbi = hmdfs_sb(child_dentry->d_sb);
994 	struct path src_path;
995 	struct inode *child_inode = NULL;
996 
997 	trace_hmdfs_lookup_share(parent_inode, child_dentry, flags);
998 	if (d_name->len > NAME_MAX) {
999 		ret = ERR_PTR(-ENAMETOOLONG);
1000 		goto err_out;
1001 	}
1002 
1003 	err = init_hmdfs_dentry_info(sbi, child_dentry, HMDFS_LAYER_OTHER_LOCAL);
1004 	if (err) {
1005 		ret = ERR_PTR(err);
1006 		goto err_out;
1007 	}
1008 
1009 	err = get_path_from_share_table(sbi, child_dentry, &src_path);
1010 	if (err) {
1011 		ret = ERR_PTR(err);
1012 		goto err_out;
1013 	}
1014 
1015 	hmdfs_set_lower_path(child_dentry, &src_path);
1016 	child_inode = fill_inode_local(parent_inode->i_sb,
1017 					d_inode(src_path.dentry), d_name->name);
1018 
1019 	set_sharefile_flag(hmdfs_d(child_dentry));
1020 
1021 	if (IS_ERR(child_inode)) {
1022 		err = PTR_ERR(child_inode);
1023 		ret = ERR_PTR(err);
1024 		hmdfs_put_reset_lower_path(child_dentry);
1025 		goto err_out;
1026 	}
1027 	ret = d_splice_alias(child_inode, child_dentry);
1028 	if (IS_ERR(ret)) {
1029 		err = PTR_ERR(ret);
1030 		hmdfs_put_reset_lower_path(child_dentry);
1031 		goto err_out;
1032 	}
1033 
1034 	check_and_fixup_ownership(parent_inode, child_inode);
1035 
1036 err_out:
1037 	trace_hmdfs_lookup_share_end(parent_inode, child_dentry, err);
1038 	return ret;
1039 }
1040 
1041 const struct inode_operations hmdfs_dir_inode_ops_local = {
1042 	.lookup = hmdfs_lookup_local,
1043 	.mkdir = hmdfs_mkdir_local,
1044 	.create = hmdfs_create_local,
1045 	.rmdir = hmdfs_rmdir_local,
1046 	.unlink = hmdfs_unlink_local,
1047 	.symlink = hmdfs_symlink_local,
1048 	.rename = hmdfs_rename_local,
1049 	.permission = hmdfs_permission,
1050 	.setattr = hmdfs_setattr_local,
1051 	.getattr = hmdfs_getattr_local,
1052 };
1053 
1054 const struct inode_operations hmdfs_symlink_iops_local = {
1055 	.get_link = hmdfs_get_link_local,
1056 	.permission = hmdfs_permission,
1057 	.setattr = hmdfs_setattr_local,
1058 };
1059 
1060 const struct inode_operations hmdfs_dir_inode_ops_share = {
1061 	.lookup = hmdfs_lookup_share,
1062 	.permission = hmdfs_permission,
1063 };
1064 
1065 const struct inode_operations hmdfs_file_iops_local = {
1066 	.setattr = hmdfs_setattr_local,
1067 	.getattr = hmdfs_getattr_local,
1068 	.permission = hmdfs_permission,
1069 	.listxattr = hmdfs_local_listxattr,
1070 };
1071