• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/hmdfs/inode_merge.c
4  *
5  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
6  */
7 
8 #include "hmdfs_merge_view.h"
9 #include <linux/atomic.h>
10 #include <linux/fs.h>
11 #include <linux/fs_stack.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/mount.h>
15 #include <linux/namei.h>
16 #include <linux/rwsem.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include "authority/authentication.h"
20 #include "hmdfs_trace.h"
21 
22 struct kmem_cache *hmdfs_dentry_merge_cachep;
23 
hmdfs_get_fst_lo_d(struct dentry * dentry)24 struct dentry *hmdfs_get_fst_lo_d(struct dentry *dentry)
25 {
26 	struct hmdfs_dentry_info_merge *dim = hmdfs_dm(dentry);
27 	struct hmdfs_dentry_comrade *comrade = NULL;
28 	struct dentry *d = NULL;
29 
30 	mutex_lock(&dim->comrade_list_lock);
31 	comrade = list_first_entry_or_null(&dim->comrade_list,
32 					   struct hmdfs_dentry_comrade, list);
33 	if (comrade)
34 		d = dget(comrade->lo_d);
35 	mutex_unlock(&dim->comrade_list_lock);
36 	return d;
37 }
38 
hmdfs_get_lo_d(struct dentry * dentry,int dev_id)39 struct dentry *hmdfs_get_lo_d(struct dentry *dentry, int dev_id)
40 {
41 	struct hmdfs_dentry_info_merge *dim = hmdfs_dm(dentry);
42 	struct hmdfs_dentry_comrade *comrade = NULL;
43 	struct dentry *d = NULL;
44 
45 	mutex_lock(&dim->comrade_list_lock);
46 	list_for_each_entry(comrade, &dim->comrade_list, list) {
47 		if (comrade->dev_id == dev_id) {
48 			d = dget(comrade->lo_d);
49 			break;
50 		}
51 	}
52 	mutex_unlock(&dim->comrade_list_lock);
53 	return d;
54 }
55 
update_inode_attr(struct inode * inode,struct dentry * child_dentry)56 static void update_inode_attr(struct inode *inode, struct dentry *child_dentry)
57 {
58 	struct inode *li = NULL;
59 	struct hmdfs_dentry_info_merge *cdi = hmdfs_dm(child_dentry);
60 	struct hmdfs_dentry_comrade *comrade = NULL;
61 	struct hmdfs_dentry_comrade *fst_comrade = NULL;
62 
63 	mutex_lock(&cdi->comrade_list_lock);
64 	fst_comrade = list_first_entry(&cdi->comrade_list,
65 				       struct hmdfs_dentry_comrade, list);
66 	list_for_each_entry(comrade, &cdi->comrade_list, list) {
67 		li = d_inode(comrade->lo_d);
68 		if (!li)
69 			continue;
70 
71 		if (comrade == fst_comrade) {
72 			inode->i_atime = li->i_atime;
73 			inode->i_ctime = li->i_ctime;
74 			inode->i_mtime = li->i_mtime;
75 			inode->i_size = li->i_size;
76 			continue;
77 		}
78 
79 		if (hmdfs_time_compare(&inode->i_mtime, &li->i_mtime) < 0)
80 			inode->i_mtime = li->i_mtime;
81 	}
82 	mutex_unlock(&cdi->comrade_list_lock);
83 }
84 
get_num_comrades(struct dentry * dentry)85 static int get_num_comrades(struct dentry *dentry)
86 {
87 	struct list_head *pos;
88 	struct hmdfs_dentry_info_merge *dim = hmdfs_dm(dentry);
89 	int count = 0;
90 
91 	mutex_lock(&dim->comrade_list_lock);
92 	list_for_each(pos, &dim->comrade_list)
93 		count++;
94 	mutex_unlock(&dim->comrade_list_lock);
95 	return count;
96 }
97 
fill_inode_merge(struct super_block * sb,struct inode * parent_inode,struct dentry * child_dentry,struct dentry * lo_d_dentry)98 static struct inode *fill_inode_merge(struct super_block *sb,
99 				      struct inode *parent_inode,
100 				      struct dentry *child_dentry,
101 				      struct dentry *lo_d_dentry)
102 {
103 	int ret = 0;
104 	struct dentry *fst_lo_d = NULL;
105 	struct hmdfs_inode_info *info = NULL;
106 	struct inode *inode = NULL;
107 	umode_t mode;
108 
109 	if (lo_d_dentry) {
110 		fst_lo_d = lo_d_dentry;
111 		dget(fst_lo_d);
112 	} else {
113 		fst_lo_d = hmdfs_get_fst_lo_d(child_dentry);
114 	}
115 	if (!fst_lo_d) {
116 		inode = ERR_PTR(-EINVAL);
117 		goto out;
118 	}
119 	if (hmdfs_i(parent_inode)->inode_type == HMDFS_LAYER_ZERO)
120 		inode = hmdfs_iget_locked_root(sb, HMDFS_ROOT_MERGE, NULL,
121 					       NULL);
122 	else
123 		inode = hmdfs_iget5_locked_merge(sb, fst_lo_d);
124 	if (!inode) {
125 		hmdfs_err("iget5_locked get inode NULL");
126 		inode = ERR_PTR(-ENOMEM);
127 		goto out;
128 	}
129 	if (!(inode->i_state & I_NEW))
130 		goto out;
131 	info = hmdfs_i(inode);
132 	if (hmdfs_i(parent_inode)->inode_type == HMDFS_LAYER_ZERO)
133 		info->inode_type = HMDFS_LAYER_FIRST_MERGE;
134 	else
135 		info->inode_type = HMDFS_LAYER_OTHER_MERGE;
136 
137 	inode->i_uid = KUIDT_INIT((uid_t)1000);
138 	inode->i_gid = KGIDT_INIT((gid_t)1000);
139 
140 	update_inode_attr(inode, child_dentry);
141 	mode = d_inode(fst_lo_d)->i_mode;
142 
143 	if (S_ISREG(mode)) {
144 		inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
145 		inode->i_op = &hmdfs_file_iops_merge;
146 		inode->i_fop = &hmdfs_file_fops_merge;
147 		set_nlink(inode, 1);
148 	} else if (S_ISDIR(mode)) {
149 		inode->i_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IXOTH;
150 		inode->i_op = &hmdfs_dir_iops_merge;
151 		inode->i_fop = &hmdfs_dir_fops_merge;
152 		set_nlink(inode, get_num_comrades(child_dentry) + 2);
153 	} else {
154 		ret = -EIO;
155 		goto bad_inode;
156 	}
157 
158 	unlock_new_inode(inode);
159 out:
160 	dput(fst_lo_d);
161 	return inode;
162 bad_inode:
163 	iget_failed(inode);
164 	return ERR_PTR(ret);
165 }
166 
alloc_comrade(struct dentry * lo_d,int dev_id)167 struct hmdfs_dentry_comrade *alloc_comrade(struct dentry *lo_d, int dev_id)
168 {
169 	struct hmdfs_dentry_comrade *comrade = NULL;
170 
171 	// 文件只有一个 comrade,考虑 {comrade, list + list lock}
172 	comrade = kzalloc(sizeof(*comrade), GFP_KERNEL);
173 	if (unlikely(!comrade))
174 		return ERR_PTR(-ENOMEM);
175 
176 	comrade->lo_d = lo_d;
177 	comrade->dev_id = dev_id;
178 	dget(lo_d);
179 	return comrade;
180 }
181 
link_comrade(struct list_head * onstack_comrades_head,struct hmdfs_dentry_comrade * comrade)182 void link_comrade(struct list_head *onstack_comrades_head,
183 		  struct hmdfs_dentry_comrade *comrade)
184 {
185 	struct hmdfs_dentry_comrade *c = NULL;
186 
187 	list_for_each_entry(c, onstack_comrades_head, list) {
188 		if (likely(c->dev_id != comrade->dev_id))
189 			continue;
190 		hmdfs_err("Redundant comrade of device %llu", c->dev_id);
191 		dput(comrade->lo_d);
192 		kfree(comrade);
193 		WARN_ON(1);
194 		return;
195 	}
196 
197 	if (comrade_is_local(comrade))
198 		list_add(&comrade->list, onstack_comrades_head);
199 	else
200 		list_add_tail(&comrade->list, onstack_comrades_head);
201 }
202 
203 /**
204  * assign_comrades_unlocked - assign a child dentry with comrades
205  *
206  * We tend to setup a local list of all the comrades we found and place the
207  * list onto the dentry_info to achieve atomicity.
208  */
assign_comrades_unlocked(struct dentry * child_dentry,struct list_head * onstack_comrades_head)209 static void assign_comrades_unlocked(struct dentry *child_dentry,
210 				     struct list_head *onstack_comrades_head)
211 {
212 	struct hmdfs_dentry_info_merge *cdi = hmdfs_dm(child_dentry);
213 
214 	mutex_lock(&cdi->comrade_list_lock);
215 	WARN_ON(!list_empty(&cdi->comrade_list));
216 	list_splice_init(onstack_comrades_head, &cdi->comrade_list);
217 	mutex_unlock(&cdi->comrade_list_lock);
218 }
219 
lookup_comrade(struct path lower_path,const char * d_name,int dev_id,unsigned int flags)220 static struct hmdfs_dentry_comrade *lookup_comrade(struct path lower_path,
221 						   const char *d_name,
222 						   int dev_id,
223 						   unsigned int flags)
224 {
225 	struct path path;
226 	struct hmdfs_dentry_comrade *comrade = NULL;
227 	int err;
228 
229 	err = vfs_path_lookup(lower_path.dentry, lower_path.mnt, d_name, flags,
230 			      &path);
231 	if (err)
232 		return ERR_PTR(err);
233 
234 	comrade = alloc_comrade(path.dentry, dev_id);
235 	path_put(&path);
236 	return comrade;
237 }
238 
239 /**
240  * conf_name_trans_nop - do nothing but copy
241  *
242  * WARNING: always check before translation
243  */
conf_name_trans_nop(struct dentry * d)244 static char *conf_name_trans_nop(struct dentry *d)
245 {
246 	return kstrndup(d->d_name.name, d->d_name.len, GFP_KERNEL);
247 }
248 
249 /**
250  * conf_name_trans_dir - conflicted name translation for directory
251  *
252  * WARNING: always check before translation
253  */
conf_name_trans_dir(struct dentry * d)254 static char *conf_name_trans_dir(struct dentry *d)
255 {
256 	int len = d->d_name.len - strlen(CONFLICTING_DIR_SUFFIX);
257 
258 	return kstrndup(d->d_name.name, len, GFP_KERNEL);
259 }
260 
261 /**
262  * conf_name_trans_reg - conflicted name translation for regular file
263  *
264  * WARNING: always check before translation
265  */
conf_name_trans_reg(struct dentry * d,int * dev_id)266 static char *conf_name_trans_reg(struct dentry *d, int *dev_id)
267 {
268 	int dot_pos, start_cpy_pos, num_len, i;
269 	int len = d->d_name.len;
270 	char *name = kstrndup(d->d_name.name, d->d_name.len, GFP_KERNEL);
271 
272 	if (unlikely(!name))
273 		return NULL;
274 
275 	// find the last dot if possible
276 	for (dot_pos = len - 1; dot_pos >= 0; dot_pos--) {
277 		if (name[dot_pos] == '.')
278 			break;
279 	}
280 	if (dot_pos == -1)
281 		dot_pos = len;
282 
283 	// retrieve the conf sn (i.e. dev_id)
284 	num_len = 0;
285 	for (i = dot_pos - 1; i >= 0; i--) {
286 		if (name[i] >= '0' && name[i] <= '9')
287 			num_len++;
288 		else
289 			break;
290 	}
291 
292 	*dev_id = 0;
293 	for (i = 0; i < num_len; i++)
294 		*dev_id = *dev_id * 10 + name[dot_pos - num_len + i] - '0';
295 
296 	// move the file suffix( '\0' included) right after the file name
297 	start_cpy_pos =
298 		dot_pos - num_len - strlen(CONFLICTING_FILE_CONST_SUFFIX);
299 	memmove(name + start_cpy_pos, name + dot_pos, len - dot_pos + 1);
300 	return name;
301 }
302 
check_filename(const char * name,int len)303 int check_filename(const char *name, int len)
304 {
305 	int cmp_res = 0;
306 
307 	if (len >= strlen(CONFLICTING_DIR_SUFFIX)) {
308 		cmp_res = strncmp(name + len - strlen(CONFLICTING_DIR_SUFFIX),
309 				  CONFLICTING_DIR_SUFFIX,
310 				  strlen(CONFLICTING_DIR_SUFFIX));
311 		if (cmp_res == 0)
312 			return DT_DIR;
313 	}
314 
315 	if (len >= strlen(CONFLICTING_FILE_CONST_SUFFIX)) {
316 		int dot_pos, start_cmp_pos, num_len, i;
317 
318 		for (dot_pos = len - 1; dot_pos >= 0; dot_pos--) {
319 			if (name[dot_pos] == '.')
320 				break;
321 		}
322 		if (dot_pos == -1)
323 			dot_pos = len;
324 
325 		num_len = 0;
326 		for (i = dot_pos - 1; i >= 0; i--) {
327 			if (name[i] >= '0' && name[i] <= '9')
328 				num_len++;
329 			else
330 				break;
331 		}
332 
333 		start_cmp_pos = dot_pos - num_len -
334 				strlen(CONFLICTING_FILE_CONST_SUFFIX);
335 		cmp_res = strncmp(name + start_cmp_pos,
336 				  CONFLICTING_FILE_CONST_SUFFIX,
337 				  strlen(CONFLICTING_FILE_CONST_SUFFIX));
338 		if (cmp_res == 0)
339 			return DT_REG;
340 	}
341 
342 	return 0;
343 }
344 
merge_lookup_comrade(struct hmdfs_sb_info * sbi,const char * name,int devid,unsigned int flags)345 static struct hmdfs_dentry_comrade *merge_lookup_comrade(
346 	struct hmdfs_sb_info *sbi, const char *name, int devid,
347 	unsigned int flags)
348 {
349 	int err;
350 	struct path root, path;
351 	struct hmdfs_dentry_comrade *comrade = NULL;
352 	const struct cred *old_cred = hmdfs_override_creds(sbi->cred);
353 
354 	err = kern_path(sbi->real_dst, LOOKUP_DIRECTORY, &root);
355 	if (err) {
356 		comrade = ERR_PTR(err);
357 		goto out;
358 	}
359 
360 	err = vfs_path_lookup(root.dentry, root.mnt, name, flags, &path);
361 	if (err) {
362 		comrade = ERR_PTR(err);
363 		goto root_put;
364 	}
365 
366 	comrade = alloc_comrade(path.dentry, devid);
367 
368 	path_put(&path);
369 root_put:
370 	path_put(&root);
371 out:
372 	hmdfs_revert_creds(old_cred);
373 	return comrade;
374 }
375 
is_valid_comrade(struct hmdfs_dentry_info_merge * mdi,umode_t mode)376 static bool is_valid_comrade(struct hmdfs_dentry_info_merge *mdi, umode_t mode)
377 {
378 	if (mdi->type == DT_UNKNOWN) {
379 		mdi->type = S_ISDIR(mode) ? DT_DIR : DT_REG;
380 		return true;
381 	}
382 
383 	if (mdi->type == DT_DIR && S_ISDIR(mode)) {
384 		return true;
385 	}
386 
387 	if (mdi->type == DT_REG && list_empty(&mdi->comrade_list) &&
388 		!S_ISDIR(mode)) {
389 		return true;
390 	}
391 
392 	return false;
393 }
394 
merge_lookup_work_func(struct work_struct * work)395 static void merge_lookup_work_func(struct work_struct *work)
396 {
397 	struct merge_lookup_work *ml_work;
398 	struct hmdfs_dentry_comrade *comrade;
399 	struct hmdfs_dentry_info_merge *mdi;
400 	int found = false;
401 
402 	ml_work = container_of(work, struct merge_lookup_work, work);
403 	mdi = container_of(ml_work->wait_queue,	struct hmdfs_dentry_info_merge,
404 		wait_queue);
405 
406 	trace_hmdfs_merge_lookup_work_enter(ml_work);
407 
408 	comrade = merge_lookup_comrade(ml_work->sbi, ml_work->name,
409 		ml_work->devid, ml_work->flags);
410 	if (IS_ERR(comrade)) {
411 		mutex_lock(&mdi->work_lock);
412 		goto out;
413 	}
414 
415 	mutex_lock(&mdi->work_lock);
416 	mutex_lock(&mdi->comrade_list_lock);
417 	if (!is_valid_comrade(mdi, hmdfs_cm(comrade))) {
418 		destroy_comrade(comrade);
419 	} else {
420 		found = true;
421 		link_comrade(&mdi->comrade_list, comrade);
422 	}
423 	mutex_unlock(&mdi->comrade_list_lock);
424 
425 out:
426 	if (--mdi->work_count == 0 || found)
427 		wake_up_all(ml_work->wait_queue);
428 	mutex_unlock(&mdi->work_lock);
429 
430 	trace_hmdfs_merge_lookup_work_exit(ml_work, found);
431 	kfree(ml_work->name);
432 	kfree(ml_work);
433 }
434 
merge_lookup_async(struct hmdfs_dentry_info_merge * mdi,struct hmdfs_sb_info * sbi,int devid,const char * name,unsigned int flags)435 static int merge_lookup_async(struct hmdfs_dentry_info_merge *mdi,
436 	struct hmdfs_sb_info *sbi, int devid, const char *name,
437 	unsigned int flags)
438 {
439 	int err = -ENOMEM;
440 	struct merge_lookup_work *ml_work;
441 
442 	ml_work = kmalloc(sizeof(*ml_work), GFP_KERNEL);
443 	if (!ml_work)
444 		goto out;
445 
446 	ml_work->name = kstrdup(name, GFP_KERNEL);
447 	if (!ml_work->name) {
448 		kfree(ml_work);
449 		goto out;
450 	}
451 
452 	ml_work->devid = devid;
453 	ml_work->flags = flags;
454 	ml_work->sbi = sbi;
455 	ml_work->wait_queue = &mdi->wait_queue;
456 	INIT_WORK(&ml_work->work, merge_lookup_work_func);
457 
458 	schedule_work(&ml_work->work);
459 	++mdi->work_count;
460 	err = 0;
461 out:
462 	return err;
463 }
464 
hmdfs_get_real_dname(struct dentry * dentry,int * devid,int * type)465 static char *hmdfs_get_real_dname(struct dentry *dentry, int *devid, int *type)
466 {
467 	char *rname;
468 
469 	*type = check_filename(dentry->d_name.name, dentry->d_name.len);
470 	if (*type == DT_REG)
471 		rname = conf_name_trans_reg(dentry, devid);
472 	else if (*type == DT_DIR)
473 		rname = conf_name_trans_dir(dentry);
474 	else
475 		rname = conf_name_trans_nop(dentry);
476 
477 	return rname;
478 }
479 
lookup_merge_normal(struct dentry * dentry,unsigned int flags)480 static int lookup_merge_normal(struct dentry *dentry, unsigned int flags)
481 {
482 	int ret = -ENOMEM;
483 	int err = 0;
484 	int devid = -1;
485 	struct dentry *pdentry = dget_parent(dentry);
486 	struct hmdfs_dentry_info_merge *mdi = hmdfs_dm(dentry);
487 	struct hmdfs_sb_info *sbi = hmdfs_sb(dentry->d_sb);
488 	struct hmdfs_peer *peer;
489 	char *rname, *ppath, *cpath;
490 
491 	rname = hmdfs_get_real_dname(dentry, &devid, &mdi->type);
492 	if (unlikely(!rname)) {
493 		goto out;
494 	}
495 
496 	ppath = hmdfs_merge_get_dentry_relative_path(pdentry);
497 	if (unlikely(!ppath)) {
498 		hmdfs_err("failed to get parent relative path");
499 		goto out_rname;
500 	}
501 
502 	cpath = kzalloc(PATH_MAX, GFP_KERNEL);
503 	if (unlikely(!cpath)) {
504 		hmdfs_err("failed to get child device_view path");
505 		goto out_ppath;
506 	}
507 
508 	mutex_lock(&mdi->work_lock);
509 	mutex_lock(&sbi->connections.node_lock);
510 	if (mdi->type != DT_REG || devid == 0) {
511 		snprintf(cpath, PATH_MAX, "device_view/local%s/%s", ppath,
512 			rname);
513 		err = merge_lookup_async(mdi, sbi, 0, cpath, flags);
514 		if (err)
515 			hmdfs_err("failed to create local lookup work");
516 	}
517 
518 	list_for_each_entry(peer, &sbi->connections.node_list, list) {
519 		if (mdi->type == DT_REG && peer->device_id != devid)
520 			continue;
521 		snprintf(cpath, PATH_MAX, "device_view/%s%s/%s", peer->cid,
522 			ppath, rname);
523 		err = merge_lookup_async(mdi, sbi, peer->device_id, cpath,
524 			flags);
525 		if (err)
526 			hmdfs_err("failed to create remote lookup work");
527 	}
528 	mutex_unlock(&sbi->connections.node_lock);
529 	mutex_unlock(&mdi->work_lock);
530 
531 	wait_event(mdi->wait_queue, is_merge_lookup_end(mdi));
532 
533 	ret = -ENOENT;
534 	if (!is_comrade_list_empty(mdi))
535 		ret = 0;
536 
537 	kfree(cpath);
538 out_ppath:
539 	kfree(ppath);
540 out_rname:
541 	kfree(rname);
542 out:
543 	dput(pdentry);
544 	return ret;
545 }
546 
547 /**
548  * do_lookup_merge_root - lookup the root of the merge view(root/merge_view)
549  *
550  * It's common for a network filesystem to incur various of faults, so we
551  * intent to show mercy for faults here, except faults reported by the local.
552  */
do_lookup_merge_root(struct path path_dev,struct dentry * child_dentry,unsigned int flags)553 static int do_lookup_merge_root(struct path path_dev,
554 				struct dentry *child_dentry, unsigned int flags)
555 {
556 	struct hmdfs_sb_info *sbi = hmdfs_sb(child_dentry->d_sb);
557 	struct hmdfs_dentry_comrade *comrade;
558 	const int buf_len =
559 		max((int)HMDFS_CID_SIZE + 1, (int)sizeof(DEVICE_VIEW_LOCAL));
560 	char *buf = kzalloc(buf_len, GFP_KERNEL);
561 	struct hmdfs_peer *peer;
562 	LIST_HEAD(head);
563 	int ret;
564 
565 	if (!buf)
566 		return -ENOMEM;
567 
568 	// lookup real_dst/device_view/local
569 	memcpy(buf, DEVICE_VIEW_LOCAL, sizeof(DEVICE_VIEW_LOCAL));
570 	comrade = lookup_comrade(path_dev, buf, HMDFS_DEVID_LOCAL, flags);
571 	if (IS_ERR(comrade)) {
572 		ret = PTR_ERR(comrade);
573 		goto out;
574 	}
575 	link_comrade(&head, comrade);
576 
577 	// lookup real_dst/device_view/cidxx
578 	mutex_lock(&sbi->connections.node_lock);
579 	list_for_each_entry(peer, &sbi->connections.node_list, list) {
580 		mutex_unlock(&sbi->connections.node_lock);
581 		memcpy(buf, peer->cid, HMDFS_CID_SIZE);
582 		comrade = lookup_comrade(path_dev, buf, peer->device_id, flags);
583 		if (IS_ERR(comrade))
584 			continue;
585 
586 		link_comrade(&head, comrade);
587 		mutex_lock(&sbi->connections.node_lock);
588 	}
589 	mutex_unlock(&sbi->connections.node_lock);
590 
591 	assign_comrades_unlocked(child_dentry, &head);
592 	ret = 0;
593 
594 out:
595 	kfree(buf);
596 	return ret;
597 }
598 
599 // mkdir -p
lock_root_inode_shared(struct inode * root,bool * locked,bool * down)600 static void lock_root_inode_shared(struct inode *root, bool *locked, bool *down)
601 {
602 	struct rw_semaphore *sem = &root->i_rwsem;
603 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
604 #define RWSEM_READER_OWNED     (1UL << 0)
605 #define RWSEM_RD_NONSPINNABLE  (1UL << 1)
606 #define RWSEM_WR_NONSPINNABLE  (1UL << 2)
607 #define RWSEM_NONSPINNABLE     (RWSEM_RD_NONSPINNABLE | RWSEM_WR_NONSPINNABLE)
608 #define RWSEM_OWNER_FLAGS_MASK (RWSEM_READER_OWNED | RWSEM_NONSPINNABLE)
609 	struct task_struct *sem_owner =
610 		(struct task_struct *)(atomic_long_read(&sem->owner) &
611 				       ~RWSEM_OWNER_FLAGS_MASK);
612 #else
613 	struct task_struct *sem_owner = sem->owner;
614 #endif
615 
616 	*locked = false;
617 	*down = false;
618 
619 	if (sem_owner != current)
620 		return;
621 
622 	// It's us that takes the wsem
623 	if (!inode_trylock_shared(root)) {
624 		downgrade_write(sem);
625 		*down = true;
626 	}
627 	*locked = true;
628 }
629 
restore_root_inode_sem(struct inode * root,bool locked,bool down)630 static void restore_root_inode_sem(struct inode *root, bool locked, bool down)
631 {
632 	if (!locked)
633 		return;
634 
635 	inode_unlock_shared(root);
636 	if (down)
637 		inode_lock(root);
638 }
639 
lookup_merge_root(struct inode * root_inode,struct dentry * child_dentry,unsigned int flags)640 static int lookup_merge_root(struct inode *root_inode,
641 			     struct dentry *child_dentry, unsigned int flags)
642 {
643 	struct hmdfs_sb_info *sbi = hmdfs_sb(child_dentry->d_sb);
644 	struct path path_dev;
645 	int ret = -ENOENT;
646 	int buf_len;
647 	char *buf = NULL;
648 	bool locked, down;
649 
650 	// consider additional one slash and one '\0'
651 	buf_len = strlen(sbi->real_dst) + 1 + sizeof(DEVICE_VIEW_ROOT);
652 	if (buf_len > PATH_MAX)
653 		return -ENAMETOOLONG;
654 
655 	buf = kmalloc(buf_len, GFP_KERNEL);
656 	if (unlikely(!buf))
657 		return -ENOMEM;
658 
659 	sprintf(buf, "%s/%s", sbi->real_dst, DEVICE_VIEW_ROOT);
660 	lock_root_inode_shared(root_inode, &locked, &down);
661 	ret = hmdfs_get_path_in_sb(child_dentry->d_sb, buf, LOOKUP_DIRECTORY,
662 				   &path_dev);
663 	if (ret)
664 		goto free_buf;
665 
666 	ret = do_lookup_merge_root(path_dev, child_dentry, flags);
667 	path_put(&path_dev);
668 
669 free_buf:
670 	kfree(buf);
671 	restore_root_inode_sem(root_inode, locked, down);
672 	return ret;
673 }
674 
init_hmdfs_dentry_info_merge(struct hmdfs_sb_info * sbi,struct dentry * dentry)675 int init_hmdfs_dentry_info_merge(struct hmdfs_sb_info *sbi,
676 	struct dentry *dentry)
677 {
678 	struct hmdfs_dentry_info_merge *mdi = NULL;
679 
680 	mdi = kmem_cache_zalloc(hmdfs_dentry_merge_cachep, GFP_NOFS);
681 	if (!mdi)
682 		return -ENOMEM;
683 
684 	mdi->ctime = jiffies;
685 	mdi->type = DT_UNKNOWN;
686 	mdi->work_count = 0;
687 	mutex_init(&mdi->work_lock);
688 	init_waitqueue_head(&mdi->wait_queue);
689 	INIT_LIST_HEAD(&mdi->comrade_list);
690 	mutex_init(&mdi->comrade_list_lock);
691 
692 	d_set_d_op(dentry, &hmdfs_dops_merge);
693 	dentry->d_fsdata = mdi;
694 	return 0;
695 }
696 
697 // do this in a map-reduce manner
hmdfs_lookup_merge(struct inode * parent_inode,struct dentry * child_dentry,unsigned int flags)698 struct dentry *hmdfs_lookup_merge(struct inode *parent_inode,
699 				  struct dentry *child_dentry,
700 				  unsigned int flags)
701 {
702 	bool create = flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET);
703 	struct hmdfs_sb_info *sbi = hmdfs_sb(child_dentry->d_sb);
704 	struct hmdfs_inode_info *pii = hmdfs_i(parent_inode);
705 	struct inode *child_inode = NULL;
706 	struct dentry *ret_dentry = NULL;
707 	int err = 0;
708 
709 	/*
710 	 * Internal flags like LOOKUP_CREATE should not pass to device view.
711 	 * LOOKUP_REVAL is needed because dentry cache in hmdfs might be stale
712 	 * after rename in lower fs. LOOKUP_DIRECTORY is not needed because
713 	 * merge_view can do the judgement that whether result is directory or
714 	 * not.
715 	 */
716 	flags = flags & LOOKUP_REVAL;
717 
718 	child_dentry->d_fsdata = NULL;
719 
720 	if (child_dentry->d_name.len > NAME_MAX) {
721 		err = -ENAMETOOLONG;
722 		goto out;
723 	}
724 
725 	err = init_hmdfs_dentry_info_merge(sbi, child_dentry);
726 	if (unlikely(err))
727 		goto out;
728 
729 	if (pii->inode_type == HMDFS_LAYER_ZERO) {
730 		hmdfs_dm(child_dentry)->dentry_type = HMDFS_LAYER_FIRST_MERGE;
731 		err = lookup_merge_root(parent_inode, child_dentry, flags);
732 	} else {
733 		hmdfs_dm(child_dentry)->dentry_type = HMDFS_LAYER_OTHER_MERGE;
734 		err = lookup_merge_normal(child_dentry, flags);
735 	}
736 
737 	if (!err) {
738 		struct hmdfs_inode_info *info = NULL;
739 
740 		child_inode = fill_inode_merge(parent_inode->i_sb, parent_inode,
741 					       child_dentry, NULL);
742 		ret_dentry = d_splice_alias(child_inode, child_dentry);
743 		if (IS_ERR(ret_dentry)) {
744 			clear_comrades(child_dentry);
745 			err = PTR_ERR(ret_dentry);
746 			goto out;
747 		}
748 		if (ret_dentry) {
749 			child_dentry = ret_dentry;
750 		}
751 		info = hmdfs_i(child_inode);
752 		if (info->inode_type == HMDFS_LAYER_FIRST_MERGE)
753 			hmdfs_root_inode_perm_init(child_inode);
754 		else
755 			check_and_fixup_ownership_remote(parent_inode,
756 							 child_dentry);
757 
758 		goto out;
759 	}
760 
761 	if ((err == -ENOENT) && create)
762 		err = 0;
763 
764 out:
765 	hmdfs_trace_merge(trace_hmdfs_lookup_merge_end, parent_inode,
766 			  child_dentry, err);
767 	return err ? ERR_PTR(err) : ret_dentry;
768 }
769 
hmdfs_getattr_merge(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)770 static int hmdfs_getattr_merge(const struct path *path, struct kstat *stat,
771 			       u32 request_mask, unsigned int flags)
772 {
773 	int ret;
774 	struct path lower_path = {
775 		.dentry = hmdfs_get_fst_lo_d(path->dentry),
776 		.mnt = path->mnt,
777 	};
778 
779 	if (unlikely(!lower_path.dentry)) {
780 		hmdfs_err("Fatal! No comrades");
781 		ret = -EINVAL;
782 		goto out;
783 	}
784 
785 	ret = vfs_getattr(&lower_path, stat, request_mask, flags);
786 out:
787 	dput(lower_path.dentry);
788 	return ret;
789 }
790 
hmdfs_setattr_merge(struct dentry * dentry,struct iattr * ia)791 static int hmdfs_setattr_merge(struct dentry *dentry, struct iattr *ia)
792 {
793 	struct inode *inode = d_inode(dentry);
794 	struct dentry *lower_dentry = hmdfs_get_fst_lo_d(dentry);
795 	struct inode *lower_inode = NULL;
796 	struct iattr lower_ia;
797 	unsigned int ia_valid = ia->ia_valid;
798 	int err = 0;
799 	kuid_t tmp_uid;
800 
801 	if (!lower_dentry) {
802 		WARN_ON(1);
803 		err = -EINVAL;
804 		goto out;
805 	}
806 
807 	lower_inode = d_inode(lower_dentry);
808 	memcpy(&lower_ia, ia, sizeof(lower_ia));
809 	if (ia_valid & ATTR_FILE)
810 		lower_ia.ia_file = hmdfs_f(ia->ia_file)->lower_file;
811 	lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
812 
813 	inode_lock(lower_inode);
814 	tmp_uid = hmdfs_override_inode_uid(lower_inode);
815 
816 	err = notify_change(lower_dentry, &lower_ia, NULL);
817 	i_size_write(inode, i_size_read(lower_inode));
818 	inode->i_atime = lower_inode->i_atime;
819 	inode->i_mtime = lower_inode->i_mtime;
820 	inode->i_ctime = lower_inode->i_ctime;
821 	hmdfs_revert_inode_uid(lower_inode, tmp_uid);
822 
823 	inode_unlock(lower_inode);
824 
825 out:
826 	dput(lower_dentry);
827 	return err;
828 }
829 
830 const struct inode_operations hmdfs_file_iops_merge = {
831 	.getattr = hmdfs_getattr_merge,
832 	.setattr = hmdfs_setattr_merge,
833 	.permission = hmdfs_permission,
834 };
835 
do_mkdir_merge(struct inode * parent_inode,struct dentry * child_dentry,umode_t mode,struct inode * lo_i_parent,struct dentry * lo_d_child)836 int do_mkdir_merge(struct inode *parent_inode, struct dentry *child_dentry,
837 		   umode_t mode, struct inode *lo_i_parent,
838 		   struct dentry *lo_d_child)
839 {
840 	int ret = 0;
841 	struct super_block *sb = parent_inode->i_sb;
842 	struct inode *child_inode = NULL;
843 
844 	ret = vfs_mkdir(lo_i_parent, lo_d_child, mode);
845 	if (ret)
846 		goto out;
847 
848 	child_inode =
849 		fill_inode_merge(sb, parent_inode, child_dentry, lo_d_child);
850 	if (IS_ERR(child_inode)) {
851 		ret = PTR_ERR(child_inode);
852 		goto out;
853 	}
854 	child_inode->i_uid = parent_inode->i_uid;
855 	child_inode->i_gid = parent_inode->i_gid;
856 
857 	d_add(child_dentry, child_inode);
858 	/* nlink should be increased with the joining of children */
859 	set_nlink(parent_inode, 2);
860 out:
861 	return ret;
862 }
863 
do_create_merge(struct inode * parent_inode,struct dentry * child_dentry,umode_t mode,bool want_excl,struct inode * lo_i_parent,struct dentry * lo_d_child)864 int do_create_merge(struct inode *parent_inode, struct dentry *child_dentry,
865 		    umode_t mode, bool want_excl, struct inode *lo_i_parent,
866 		    struct dentry *lo_d_child)
867 {
868 	int ret = 0;
869 	struct super_block *sb = parent_inode->i_sb;
870 	struct inode *child_inode = NULL;
871 
872 	ret = vfs_create(lo_i_parent, lo_d_child, mode, want_excl);
873 	if (ret)
874 		goto out;
875 
876 	child_inode =
877 		fill_inode_merge(sb, parent_inode, child_dentry, lo_d_child);
878 	if (IS_ERR(child_inode)) {
879 		ret = PTR_ERR(child_inode);
880 		goto out;
881 	}
882 	child_inode->i_uid = parent_inode->i_uid;
883 	child_inode->i_gid = parent_inode->i_gid;
884 
885 	d_add(child_dentry, child_inode);
886 	/* nlink should be increased with the joining of children */
887 	set_nlink(parent_inode, 2);
888 out:
889 	return ret;
890 }
891 
hmdfs_do_ops_merge(struct inode * i_parent,struct dentry * d_child,struct dentry * lo_d_child,struct path path,struct hmdfs_recursive_para * rec_op_para)892 int hmdfs_do_ops_merge(struct inode *i_parent, struct dentry *d_child,
893 		       struct dentry *lo_d_child, struct path path,
894 		       struct hmdfs_recursive_para *rec_op_para)
895 {
896 	int ret = 0;
897 
898 	if (rec_op_para->is_last) {
899 		switch (rec_op_para->opcode) {
900 		case F_MKDIR_MERGE:
901 			ret = do_mkdir_merge(i_parent, d_child,
902 					     rec_op_para->mode,
903 					     d_inode(path.dentry), lo_d_child);
904 			break;
905 		case F_CREATE_MERGE:
906 			ret = do_create_merge(i_parent, d_child,
907 					      rec_op_para->mode,
908 					      rec_op_para->want_excl,
909 					      d_inode(path.dentry), lo_d_child);
910 			break;
911 		default:
912 			ret = -EINVAL;
913 			break;
914 		}
915 	} else {
916 		ret = vfs_mkdir(d_inode(path.dentry), lo_d_child,
917 				rec_op_para->mode);
918 	}
919 	if (ret)
920 		hmdfs_err("vfs_ops failed, ops %d, err = %d",
921 			  rec_op_para->opcode, ret);
922 	return ret;
923 }
924 
hmdfs_create_lower_dentry(struct inode * i_parent,struct dentry * d_child,struct dentry * lo_d_parent,bool is_dir,struct hmdfs_recursive_para * rec_op_para)925 int hmdfs_create_lower_dentry(struct inode *i_parent, struct dentry *d_child,
926 			      struct dentry *lo_d_parent, bool is_dir,
927 			      struct hmdfs_recursive_para *rec_op_para)
928 {
929 	struct hmdfs_sb_info *sbi = i_parent->i_sb->s_fs_info;
930 	struct hmdfs_dentry_comrade *new_comrade = NULL;
931 	struct dentry *lo_d_child = NULL;
932 	char *path_buf = kmalloc(PATH_MAX, GFP_KERNEL);
933 	char *absolute_path_buf = kmalloc(PATH_MAX, GFP_KERNEL);
934 	char *path_name = NULL;
935 	struct path path = { .mnt = NULL, .dentry = NULL };
936 	int ret = 0;
937 
938 	if (unlikely(!path_buf || !absolute_path_buf)) {
939 		ret = -ENOMEM;
940 		goto out;
941 	}
942 
943 	path_name = dentry_path_raw(lo_d_parent, path_buf, PATH_MAX);
944 	if (IS_ERR(path_name)) {
945 		ret = PTR_ERR(path_name);
946 		goto out;
947 	}
948 	if ((strlen(sbi->real_dst) + strlen(path_name) +
949 	     strlen(d_child->d_name.name) + 2) > PATH_MAX) {
950 		ret = -ENAMETOOLONG;
951 		goto out;
952 	}
953 
954 	sprintf(absolute_path_buf, "%s%s/%s", sbi->real_dst, path_name,
955 		d_child->d_name.name);
956 
957 	if (is_dir)
958 		lo_d_child = kern_path_create(AT_FDCWD, absolute_path_buf,
959 					      &path, LOOKUP_DIRECTORY);
960 	else
961 		lo_d_child = kern_path_create(AT_FDCWD, absolute_path_buf,
962 					      &path, 0);
963 	if (IS_ERR(lo_d_child)) {
964 		ret = PTR_ERR(lo_d_child);
965 		goto out;
966 	}
967 	// to ensure link_comrade after vfs_mkdir succeed
968 	ret = hmdfs_do_ops_merge(i_parent, d_child, lo_d_child, path,
969 				 rec_op_para);
970 	if (ret)
971 		goto out_put;
972 	new_comrade = alloc_comrade(lo_d_child, HMDFS_DEVID_LOCAL);
973 	if (IS_ERR(new_comrade)) {
974 		ret = PTR_ERR(new_comrade);
975 		goto out_put;
976 	} else {
977 		link_comrade_unlocked(d_child, new_comrade);
978 	}
979 
980 out_put:
981 	done_path_create(&path, lo_d_child);
982 out:
983 	kfree(absolute_path_buf);
984 	kfree(path_buf);
985 	return ret;
986 }
987 
create_lo_d_parent_recur(struct dentry * d_parent,struct dentry * d_child,umode_t mode,struct hmdfs_recursive_para * rec_op_para)988 static int create_lo_d_parent_recur(struct dentry *d_parent,
989 				    struct dentry *d_child, umode_t mode,
990 				    struct hmdfs_recursive_para *rec_op_para)
991 {
992 	struct dentry *lo_d_parent, *d_pparent;
993 	struct hmdfs_dentry_info_merge *pmdi = NULL;
994 	int ret = 0;
995 
996 	pmdi = hmdfs_dm(d_parent);
997 	wait_event(pmdi->wait_queue, !has_merge_lookup_work(pmdi));
998 	lo_d_parent = hmdfs_get_lo_d(d_parent, HMDFS_DEVID_LOCAL);
999 	if (!lo_d_parent) {
1000 		d_pparent = dget_parent(d_parent);
1001 		ret = create_lo_d_parent_recur(d_pparent, d_parent,
1002 					       d_inode(d_parent)->i_mode,
1003 					       rec_op_para);
1004 		dput(d_pparent);
1005 		if (ret)
1006 			goto out;
1007 		lo_d_parent = hmdfs_get_lo_d(d_parent, HMDFS_DEVID_LOCAL);
1008 		if (!lo_d_parent) {
1009 			ret = -ENOENT;
1010 			goto out;
1011 		}
1012 	}
1013 	rec_op_para->is_last = false;
1014 	rec_op_para->mode = mode;
1015 	ret = hmdfs_create_lower_dentry(d_inode(d_parent), d_child, lo_d_parent,
1016 					true, rec_op_para);
1017 out:
1018 	dput(lo_d_parent);
1019 	return ret;
1020 }
1021 
create_lo_d_child(struct inode * i_parent,struct dentry * d_child,bool is_dir,struct hmdfs_recursive_para * rec_op_para)1022 int create_lo_d_child(struct inode *i_parent, struct dentry *d_child,
1023 		      bool is_dir, struct hmdfs_recursive_para *rec_op_para)
1024 {
1025 	struct dentry *d_pparent, *lo_d_parent, *lo_d_child;
1026 	struct dentry *d_parent = dget_parent(d_child);
1027 	struct hmdfs_dentry_info_merge *pmdi = hmdfs_dm(d_parent);
1028 	int ret = 0;
1029 	mode_t d_child_mode = rec_op_para->mode;
1030 
1031 	wait_event(pmdi->wait_queue, !has_merge_lookup_work(pmdi));
1032 
1033 	lo_d_parent = hmdfs_get_lo_d(d_parent, HMDFS_DEVID_LOCAL);
1034 	if (!lo_d_parent) {
1035 		d_pparent = dget_parent(d_parent);
1036 		ret = create_lo_d_parent_recur(d_pparent, d_parent,
1037 					       d_inode(d_parent)->i_mode,
1038 					       rec_op_para);
1039 		dput(d_pparent);
1040 		if (unlikely(ret)) {
1041 			lo_d_child = ERR_PTR(ret);
1042 			goto out;
1043 		}
1044 		lo_d_parent = hmdfs_get_lo_d(d_parent, HMDFS_DEVID_LOCAL);
1045 		if (!lo_d_parent) {
1046 			lo_d_child = ERR_PTR(-ENOENT);
1047 			goto out;
1048 		}
1049 	}
1050 	rec_op_para->is_last = true;
1051 	rec_op_para->mode = d_child_mode;
1052 	ret = hmdfs_create_lower_dentry(i_parent, d_child, lo_d_parent, is_dir,
1053 					rec_op_para);
1054 
1055 out:
1056 	dput(d_parent);
1057 	dput(lo_d_parent);
1058 	return ret;
1059 }
1060 
hmdfs_init_recursive_para(struct hmdfs_recursive_para * rec_op_para,int opcode,mode_t mode,bool want_excl,const char * name)1061 void hmdfs_init_recursive_para(struct hmdfs_recursive_para *rec_op_para,
1062 			       int opcode, mode_t mode, bool want_excl,
1063 			       const char *name)
1064 {
1065 	rec_op_para->is_last = true;
1066 	rec_op_para->opcode = opcode;
1067 	rec_op_para->mode = mode;
1068 	rec_op_para->want_excl = want_excl;
1069 	rec_op_para->name = name;
1070 }
1071 
hmdfs_mkdir_merge(struct inode * dir,struct dentry * dentry,umode_t mode)1072 int hmdfs_mkdir_merge(struct inode *dir, struct dentry *dentry, umode_t mode)
1073 {
1074 	int ret = 0;
1075 	struct hmdfs_recursive_para *rec_op_para = NULL;
1076 
1077 	// confict_name  & file_type is checked by hmdfs_mkdir_local
1078 	if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON) {
1079 		ret = -EACCES;
1080 		goto out;
1081 	}
1082 	rec_op_para = kmalloc(sizeof(*rec_op_para), GFP_KERNEL);
1083 	if (!rec_op_para) {
1084 		ret = -ENOMEM;
1085 		goto out;
1086 	}
1087 
1088 	hmdfs_init_recursive_para(rec_op_para, F_MKDIR_MERGE, mode, false,
1089 				  NULL);
1090 	ret = create_lo_d_child(dir, dentry, true, rec_op_para);
1091 out:
1092 	hmdfs_trace_merge(trace_hmdfs_mkdir_merge, dir, dentry, ret);
1093 	if (ret)
1094 		d_drop(dentry);
1095 	kfree(rec_op_para);
1096 	return ret;
1097 }
1098 
hmdfs_create_merge(struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)1099 int hmdfs_create_merge(struct inode *dir, struct dentry *dentry, umode_t mode,
1100 		       bool want_excl)
1101 {
1102 	struct hmdfs_recursive_para *rec_op_para = NULL;
1103 	int ret = 0;
1104 
1105 	rec_op_para = kmalloc(sizeof(*rec_op_para), GFP_KERNEL);
1106 	if (!rec_op_para) {
1107 		ret = -ENOMEM;
1108 		goto out;
1109 	}
1110 	hmdfs_init_recursive_para(rec_op_para, F_CREATE_MERGE, mode, want_excl,
1111 				  NULL);
1112 	// confict_name  & file_type is checked by hmdfs_create_local
1113 	ret = create_lo_d_child(dir, dentry, false, rec_op_para);
1114 out:
1115 	hmdfs_trace_merge(trace_hmdfs_create_merge, dir, dentry, ret);
1116 	if (ret)
1117 		d_drop(dentry);
1118 	kfree(rec_op_para);
1119 	return ret;
1120 }
1121 
do_rmdir_merge(struct inode * dir,struct dentry * dentry)1122 int do_rmdir_merge(struct inode *dir, struct dentry *dentry)
1123 {
1124 	int ret = 0;
1125 	struct hmdfs_dentry_info_merge *dim = hmdfs_dm(dentry);
1126 	struct hmdfs_dentry_comrade *comrade = NULL;
1127 	struct dentry *lo_d = NULL;
1128 	struct dentry *lo_d_dir = NULL;
1129 	struct inode *lo_i_dir = NULL;
1130 
1131 	wait_event(dim->wait_queue, !has_merge_lookup_work(dim));
1132 
1133 	mutex_lock(&dim->comrade_list_lock);
1134 	list_for_each_entry(comrade, &(dim->comrade_list), list) {
1135 		lo_d = comrade->lo_d;
1136 		lo_d_dir = lock_parent(lo_d);
1137 		lo_i_dir = d_inode(lo_d_dir);
1138 		ret = vfs_rmdir(lo_i_dir, lo_d);
1139 		unlock_dir(lo_d_dir);
1140 		if (ret)
1141 			break;
1142 	}
1143 	mutex_unlock(&dim->comrade_list_lock);
1144 	hmdfs_trace_merge(trace_hmdfs_rmdir_merge, dir, dentry, ret);
1145 	return ret;
1146 }
1147 
hmdfs_rmdir_merge(struct inode * dir,struct dentry * dentry)1148 int hmdfs_rmdir_merge(struct inode *dir, struct dentry *dentry)
1149 {
1150 	int ret = 0;
1151 
1152 	if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON) {
1153 		ret = -EACCES;
1154 		goto out;
1155 	}
1156 
1157 	ret = do_rmdir_merge(dir, dentry);
1158 	if (ret) {
1159 		hmdfs_err("rm dir failed:%d", ret);
1160 		goto out;
1161 	}
1162 
1163 	d_drop(dentry);
1164 out:
1165 	hmdfs_trace_merge(trace_hmdfs_rmdir_merge, dir, dentry, ret);
1166 	return ret;
1167 }
1168 
do_unlink_merge(struct inode * dir,struct dentry * dentry)1169 int do_unlink_merge(struct inode *dir, struct dentry *dentry)
1170 {
1171 	int ret = 0;
1172 	struct hmdfs_dentry_info_merge *dim = hmdfs_dm(dentry);
1173 	struct hmdfs_dentry_comrade *comrade = NULL;
1174 	struct dentry *lo_d = NULL;
1175 	struct dentry *lo_d_dir = NULL;
1176 	struct inode *lo_i_dir = NULL;
1177 
1178 	wait_event(dim->wait_queue, !has_merge_lookup_work(dim));
1179 
1180 	mutex_lock(&dim->comrade_list_lock);
1181 	list_for_each_entry(comrade, &(dim->comrade_list), list) {
1182 		lo_d = comrade->lo_d;
1183                 dget(lo_d);
1184 		lo_d_dir = lock_parent(lo_d);
1185 		lo_i_dir = d_inode(lo_d_dir);
1186 		ret = vfs_unlink(lo_i_dir, lo_d, NULL); // lo_d GET
1187 		unlock_dir(lo_d_dir);
1188                 dput(lo_d);
1189 		if (ret)
1190 			break;
1191 	}
1192 	mutex_unlock(&dim->comrade_list_lock);
1193 
1194 	return ret;
1195 }
1196 
hmdfs_unlink_merge(struct inode * dir,struct dentry * dentry)1197 int hmdfs_unlink_merge(struct inode *dir, struct dentry *dentry)
1198 {
1199 	int ret = 0;
1200 
1201 	if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON) {
1202 		ret = -EACCES;
1203 		goto out;
1204 	}
1205 
1206 	ret = do_unlink_merge(dir, dentry);
1207 	if (ret) {
1208 		hmdfs_err("unlink failed:%d", ret);
1209 		goto out;
1210 	}
1211 
1212 	d_drop(dentry);
1213 out:
1214 	return ret;
1215 }
1216 
do_rename_merge(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1217 int do_rename_merge(struct inode *old_dir, struct dentry *old_dentry,
1218 		    struct inode *new_dir, struct dentry *new_dentry,
1219 		    unsigned int flags)
1220 {
1221 	int ret = 0;
1222 	struct hmdfs_sb_info *sbi = (old_dir->i_sb)->s_fs_info;
1223 	struct hmdfs_dentry_info_merge *dim = hmdfs_dm(old_dentry);
1224 	struct hmdfs_dentry_comrade *comrade = NULL, *new_comrade = NULL;
1225 	struct path lo_p_new = { .mnt = NULL, .dentry = NULL };
1226 	struct inode *lo_i_old_dir = NULL, *lo_i_new_dir = NULL;
1227 	struct dentry *lo_d_old_dir = NULL, *lo_d_old = NULL,
1228 		      *lo_d_new_dir = NULL, *lo_d_new = NULL;
1229 	struct dentry *d_new_dir = NULL;
1230 	char *path_buf = kmalloc(PATH_MAX, GFP_KERNEL);
1231 	char *abs_path_buf = kmalloc(PATH_MAX, GFP_KERNEL);
1232 	char *path_name = NULL;
1233 	struct hmdfs_dentry_info_merge *pmdi = NULL;
1234 
1235 	if (flags & ~RENAME_NOREPLACE) {
1236 		ret = -EINVAL;
1237 		goto out;
1238 	}
1239 
1240 	if (unlikely(!path_buf || !abs_path_buf)) {
1241 		ret = -ENOMEM;
1242 		goto out;
1243 	}
1244 
1245 	wait_event(dim->wait_queue, !has_merge_lookup_work(dim));
1246 
1247 	list_for_each_entry(comrade, &dim->comrade_list, list) {
1248 		lo_d_old = comrade->lo_d;
1249 		d_new_dir = d_find_alias(new_dir);
1250 		pmdi = hmdfs_dm(d_new_dir);
1251 		wait_event(pmdi->wait_queue, !has_merge_lookup_work(pmdi));
1252 		lo_d_new_dir = hmdfs_get_lo_d(d_new_dir, comrade->dev_id);
1253 		dput(d_new_dir);
1254 
1255 		if (!lo_d_new_dir)
1256 			continue;
1257 		path_name = dentry_path_raw(lo_d_new_dir, path_buf, PATH_MAX);
1258 		dput(lo_d_new_dir);
1259 		if (IS_ERR(path_name)) {
1260 			ret = PTR_ERR(path_name);
1261 			continue;
1262 		}
1263 
1264 		if (strlen(sbi->real_dst) + strlen(path_name) +
1265 		    strlen(new_dentry->d_name.name) + 2 > PATH_MAX) {
1266 			ret = -ENAMETOOLONG;
1267 			goto out;
1268 		}
1269 
1270 		snprintf(abs_path_buf, PATH_MAX, "%s%s/%s", sbi->real_dst,
1271 			 path_name, new_dentry->d_name.name);
1272 		if (S_ISDIR(d_inode(old_dentry)->i_mode))
1273 			lo_d_new = kern_path_create(AT_FDCWD, abs_path_buf,
1274 						    &lo_p_new,
1275 						    LOOKUP_DIRECTORY);
1276 		else
1277 			lo_d_new = kern_path_create(AT_FDCWD, abs_path_buf,
1278 						    &lo_p_new, 0);
1279 		if (IS_ERR(lo_d_new))
1280 			continue;
1281 
1282 		lo_d_new_dir = dget_parent(lo_d_new);
1283 		lo_i_new_dir = d_inode(lo_d_new_dir);
1284 		lo_d_old_dir = dget_parent(lo_d_old);
1285 		lo_i_old_dir = d_inode(lo_d_old_dir);
1286 
1287 		ret = vfs_rename(lo_i_old_dir, lo_d_old, lo_i_new_dir, lo_d_new,
1288 				 NULL, flags);
1289 		new_comrade = alloc_comrade(lo_p_new.dentry, comrade->dev_id);
1290 		if (IS_ERR(new_comrade)) {
1291 			ret = PTR_ERR(new_comrade);
1292 			goto no_comrade;
1293 		}
1294 
1295 		link_comrade_unlocked(new_dentry, new_comrade);
1296 no_comrade:
1297 		done_path_create(&lo_p_new, lo_d_new);
1298 		dput(lo_d_old_dir);
1299 		dput(lo_d_new_dir);
1300 	}
1301 out:
1302 	kfree(abs_path_buf);
1303 	kfree(path_buf);
1304 	return ret;
1305 }
1306 
hmdfs_rename_merge(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1307 int hmdfs_rename_merge(struct inode *old_dir, struct dentry *old_dentry,
1308 		       struct inode *new_dir, struct dentry *new_dentry,
1309 		       unsigned int flags)
1310 {
1311 	char *old_dir_buf = NULL;
1312 	char *new_dir_buf = NULL;
1313 	char *old_dir_path = NULL;
1314 	char *new_dir_path = NULL;
1315 	struct dentry *old_dir_dentry = NULL;
1316 	struct dentry *new_dir_dentry = NULL;
1317 	int ret = 0;
1318 
1319 	if (hmdfs_file_type(old_dentry->d_name.name) != HMDFS_TYPE_COMMON ||
1320 	    hmdfs_file_type(new_dentry->d_name.name) != HMDFS_TYPE_COMMON) {
1321 		ret = -EACCES;
1322 		goto rename_out;
1323 	}
1324 	old_dir_buf = kmalloc(PATH_MAX, GFP_KERNEL);
1325 	new_dir_buf = kmalloc(PATH_MAX, GFP_KERNEL);
1326 	if (!old_dir_buf || !new_dir_buf) {
1327 		ret = -ENOMEM;
1328 		goto rename_out;
1329 	}
1330 
1331 	new_dir_dentry = d_find_alias(new_dir);
1332 	if (!new_dir_dentry) {
1333 		ret = -EINVAL;
1334 		goto rename_out;
1335 	}
1336 
1337 	old_dir_dentry = d_find_alias(old_dir);
1338 	if (!old_dir_dentry) {
1339 		ret = -EINVAL;
1340 		dput(new_dir_dentry);
1341 		goto rename_out;
1342 	}
1343 
1344 	old_dir_path = dentry_path_raw(old_dir_dentry, old_dir_buf, PATH_MAX);
1345 	new_dir_path = dentry_path_raw(new_dir_dentry, new_dir_buf, PATH_MAX);
1346 	dput(new_dir_dentry);
1347 	dput(old_dir_dentry);
1348 	if (strcmp(old_dir_path, new_dir_path)) {
1349 		ret = -EPERM;
1350 		goto rename_out;
1351 	}
1352 
1353 	trace_hmdfs_rename_merge(old_dir, old_dentry, new_dir, new_dentry,
1354 				 flags);
1355 	ret = do_rename_merge(old_dir, old_dentry, new_dir, new_dentry, flags);
1356 
1357 	if (ret != 0)
1358 		d_drop(new_dentry);
1359 
1360 	if (S_ISREG(old_dentry->d_inode->i_mode) && !ret)
1361 		d_invalidate(old_dentry);
1362 
1363 rename_out:
1364 	hmdfs_trace_rename_merge(old_dir, old_dentry, new_dir, new_dentry, ret);
1365 	kfree(old_dir_buf);
1366 	kfree(new_dir_buf);
1367 	return ret;
1368 }
1369 
1370 const struct inode_operations hmdfs_dir_iops_merge = {
1371 	.lookup = hmdfs_lookup_merge,
1372 	.mkdir = hmdfs_mkdir_merge,
1373 	.create = hmdfs_create_merge,
1374 	.rmdir = hmdfs_rmdir_merge,
1375 	.unlink = hmdfs_unlink_merge,
1376 	.rename = hmdfs_rename_merge,
1377 	.permission = hmdfs_permission,
1378 };
1379