xref: /openbmc/linux/fs/orangefs/namei.c (revision 5e7f1d433804450cdb5ba478d26742963e06b1bc)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2274dcf55SMike Marshall /*
3274dcf55SMike Marshall  * (C) 2001 Clemson University and The University of Chicago
4274dcf55SMike Marshall  *
5274dcf55SMike Marshall  * See COPYING in top-level directory.
6274dcf55SMike Marshall  */
7274dcf55SMike Marshall 
8274dcf55SMike Marshall /*
9274dcf55SMike Marshall  *  Linux VFS namei operations.
10274dcf55SMike Marshall  */
11274dcf55SMike Marshall 
12274dcf55SMike Marshall #include "protocol.h"
13575e9461SMike Marshall #include "orangefs-kernel.h"
14274dcf55SMike Marshall 
15274dcf55SMike Marshall /*
16274dcf55SMike Marshall  * Get a newly allocated inode to go with a negative dentry.
17274dcf55SMike Marshall  */
188bb8aefdSYi Liu static int orangefs_create(struct inode *dir,
19274dcf55SMike Marshall 			struct dentry *dentry,
20274dcf55SMike Marshall 			umode_t mode,
21274dcf55SMike Marshall 			bool exclusive)
22274dcf55SMike Marshall {
238bb8aefdSYi Liu 	struct orangefs_inode_s *parent = ORANGEFS_I(dir);
248bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op;
25db0267e7SMartin Brandenburg 	struct orangefs_object_kref ref;
26274dcf55SMike Marshall 	struct inode *inode;
27a55f2d86SMartin Brandenburg 	struct iattr iattr;
28274dcf55SMike Marshall 	int ret;
29274dcf55SMike Marshall 
30f66debf1SAl Viro 	gossip_debug(GOSSIP_NAME_DEBUG, "%s: %pd\n",
315253487eSMike Marshall 		     __func__,
32f66debf1SAl Viro 		     dentry);
33274dcf55SMike Marshall 
348bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_CREATE);
35274dcf55SMike Marshall 	if (!new_op)
36274dcf55SMike Marshall 		return -ENOMEM;
37274dcf55SMike Marshall 
38274dcf55SMike Marshall 	new_op->upcall.req.create.parent_refn = parent->refn;
39274dcf55SMike Marshall 
40274dcf55SMike Marshall 	fill_default_sys_attrs(new_op->upcall.req.create.attributes,
418bb8aefdSYi Liu 			       ORANGEFS_TYPE_METAFILE, mode);
42274dcf55SMike Marshall 
43274dcf55SMike Marshall 	strncpy(new_op->upcall.req.create.d_name,
446bdfb48dSXiongfeng Wang 		dentry->d_name.name, ORANGEFS_NAME_MAX - 1);
45274dcf55SMike Marshall 
46274dcf55SMike Marshall 	ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
47274dcf55SMike Marshall 
48274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
49f66debf1SAl Viro 		     "%s: %pd: handle:%pU: fsid:%d: new_op:%p: ret:%d:\n",
505253487eSMike Marshall 		     __func__,
51f66debf1SAl Viro 		     dentry,
52274dcf55SMike Marshall 		     &new_op->downcall.resp.create.refn.khandle,
535253487eSMike Marshall 		     new_op->downcall.resp.create.refn.fs_id,
545253487eSMike Marshall 		     new_op,
555253487eSMike Marshall 		     ret);
56274dcf55SMike Marshall 
575253487eSMike Marshall 	if (ret < 0)
58274dcf55SMike Marshall 		goto out;
59274dcf55SMike Marshall 
60db0267e7SMartin Brandenburg 	ref = new_op->downcall.resp.create.refn;
61db0267e7SMartin Brandenburg 
62db0267e7SMartin Brandenburg 	inode = orangefs_new_inode(dir->i_sb, dir, S_IFREG | mode, 0, &ref);
63274dcf55SMike Marshall 	if (IS_ERR(inode)) {
64f66debf1SAl Viro 		gossip_err("%s: Failed to allocate inode for file :%pd:\n",
655253487eSMike Marshall 			   __func__,
66f66debf1SAl Viro 			   dentry);
67274dcf55SMike Marshall 		ret = PTR_ERR(inode);
68274dcf55SMike Marshall 		goto out;
69274dcf55SMike Marshall 	}
70274dcf55SMike Marshall 
71274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
72f66debf1SAl Viro 		     "%s: Assigned inode :%pU: for file :%pd:\n",
735253487eSMike Marshall 		     __func__,
745253487eSMike Marshall 		     get_khandle_from_ino(inode),
75f66debf1SAl Viro 		     dentry);
76274dcf55SMike Marshall 
771e2e547aSAl Viro 	d_instantiate_new(dentry, inode);
78804b1737SMiklos Szeredi 	orangefs_set_timeout(dentry);
79274dcf55SMike Marshall 
80274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
81f66debf1SAl Viro 		     "%s: dentry instantiated for %pd\n",
825253487eSMike Marshall 		     __func__,
83f66debf1SAl Viro 		     dentry);
84274dcf55SMike Marshall 
85c2050a45SDeepa Dinamani 	dir->i_mtime = dir->i_ctime = current_time(dir);
86a55f2d86SMartin Brandenburg 	memset(&iattr, 0, sizeof iattr);
87a55f2d86SMartin Brandenburg 	iattr.ia_valid |= ATTR_MTIME;
88a55f2d86SMartin Brandenburg 	orangefs_inode_setattr(dir, &iattr);
89274dcf55SMike Marshall 	mark_inode_dirty_sync(dir);
90274dcf55SMike Marshall 	ret = 0;
91274dcf55SMike Marshall out:
9234e6148aSMike Marshall 	op_release(new_op);
935253487eSMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
94f66debf1SAl Viro 		     "%s: %pd: returning %d\n",
955253487eSMike Marshall 		     __func__,
96f66debf1SAl Viro 		     dentry,
975253487eSMike Marshall 		     ret);
98274dcf55SMike Marshall 	return ret;
99274dcf55SMike Marshall }
100274dcf55SMike Marshall 
101274dcf55SMike Marshall /*
102274dcf55SMike Marshall  * Attempt to resolve an object name (dentry->d_name), parent handle, and
103274dcf55SMike Marshall  * fsid into a handle for the object.
104274dcf55SMike Marshall  */
1058bb8aefdSYi Liu static struct dentry *orangefs_lookup(struct inode *dir, struct dentry *dentry,
106274dcf55SMike Marshall 				   unsigned int flags)
107274dcf55SMike Marshall {
1088bb8aefdSYi Liu 	struct orangefs_inode_s *parent = ORANGEFS_I(dir);
1098bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op;
110274dcf55SMike Marshall 	struct inode *inode;
111274dcf55SMike Marshall 	int ret = -EINVAL;
112274dcf55SMike Marshall 
113274dcf55SMike Marshall 	/*
114274dcf55SMike Marshall 	 * in theory we could skip a lookup here (if the intent is to
115274dcf55SMike Marshall 	 * create) in order to avoid a potentially failed lookup, but
116274dcf55SMike Marshall 	 * leaving it in can skip a valid lookup and try to create a file
117274dcf55SMike Marshall 	 * that already exists (e.g. the vfs already handles checking for
118274dcf55SMike Marshall 	 * -EEXIST on O_EXCL opens, which is broken if we skip this lookup
119274dcf55SMike Marshall 	 * in the create path)
120274dcf55SMike Marshall 	 */
121f66debf1SAl Viro 	gossip_debug(GOSSIP_NAME_DEBUG, "%s called on %pd\n",
122f66debf1SAl Viro 		     __func__, dentry);
123274dcf55SMike Marshall 
12447b4948fSMartin Brandenburg 	if (dentry->d_name.len > (ORANGEFS_NAME_MAX - 1))
125274dcf55SMike Marshall 		return ERR_PTR(-ENAMETOOLONG);
126274dcf55SMike Marshall 
1278bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_LOOKUP);
128274dcf55SMike Marshall 	if (!new_op)
129274dcf55SMike Marshall 		return ERR_PTR(-ENOMEM);
130274dcf55SMike Marshall 
1317cec28e9SMike Marshall 	new_op->upcall.req.lookup.sym_follow = ORANGEFS_LOOKUP_LINK_NO_FOLLOW;
132274dcf55SMike Marshall 
133274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG, "%s:%s:%d using parent %pU\n",
134274dcf55SMike Marshall 		     __FILE__,
135274dcf55SMike Marshall 		     __func__,
136274dcf55SMike Marshall 		     __LINE__,
137274dcf55SMike Marshall 		     &parent->refn.khandle);
138274dcf55SMike Marshall 	new_op->upcall.req.lookup.parent_refn = parent->refn;
139274dcf55SMike Marshall 
140274dcf55SMike Marshall 	strncpy(new_op->upcall.req.lookup.d_name, dentry->d_name.name,
1416bdfb48dSXiongfeng Wang 		ORANGEFS_NAME_MAX - 1);
142274dcf55SMike Marshall 
143274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
1446ceaf781SMartin Brandenburg 		     "%s: doing lookup on %s under %pU,%d\n",
145274dcf55SMike Marshall 		     __func__,
146274dcf55SMike Marshall 		     new_op->upcall.req.lookup.d_name,
147274dcf55SMike Marshall 		     &new_op->upcall.req.lookup.parent_refn.khandle,
1486ceaf781SMartin Brandenburg 		     new_op->upcall.req.lookup.parent_refn.fs_id);
149274dcf55SMike Marshall 
150274dcf55SMike Marshall 	ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
151274dcf55SMike Marshall 
152274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
153274dcf55SMike Marshall 		     "Lookup Got %pU, fsid %d (ret=%d)\n",
154274dcf55SMike Marshall 		     &new_op->downcall.resp.lookup.refn.khandle,
155274dcf55SMike Marshall 		     new_op->downcall.resp.lookup.refn.fs_id,
156274dcf55SMike Marshall 		     ret);
157274dcf55SMike Marshall 
15822fc9db2SMike Marshall 	if (ret == 0) {
159804b1737SMiklos Szeredi 		orangefs_set_timeout(dentry);
1608bb8aefdSYi Liu 		inode = orangefs_iget(dir->i_sb, &new_op->downcall.resp.lookup.refn);
16104bb1ba1SAl Viro 	} else if (ret == -ENOENT) {
16204bb1ba1SAl Viro 		inode = NULL;
16304bb1ba1SAl Viro 	} else {
16404bb1ba1SAl Viro 		/* must be a non-recoverable error */
16504bb1ba1SAl Viro 		inode = ERR_PTR(ret);
166274dcf55SMike Marshall 	}
167274dcf55SMike Marshall 
168274dcf55SMike Marshall 	op_release(new_op);
16904bb1ba1SAl Viro 	return d_splice_alias(inode, dentry);
170274dcf55SMike Marshall }
171274dcf55SMike Marshall 
172274dcf55SMike Marshall /* return 0 on success; non-zero otherwise */
1738bb8aefdSYi Liu static int orangefs_unlink(struct inode *dir, struct dentry *dentry)
174274dcf55SMike Marshall {
175274dcf55SMike Marshall 	struct inode *inode = dentry->d_inode;
1768bb8aefdSYi Liu 	struct orangefs_inode_s *parent = ORANGEFS_I(dir);
1778bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op;
178a55f2d86SMartin Brandenburg 	struct iattr iattr;
179274dcf55SMike Marshall 	int ret;
180274dcf55SMike Marshall 
181274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
182f66debf1SAl Viro 		     "%s: called on %pd\n"
183274dcf55SMike Marshall 		     "  (inode %pU): Parent is %pU | fs_id %d\n",
184274dcf55SMike Marshall 		     __func__,
185f66debf1SAl Viro 		     dentry,
186274dcf55SMike Marshall 		     get_khandle_from_ino(inode),
187274dcf55SMike Marshall 		     &parent->refn.khandle,
188274dcf55SMike Marshall 		     parent->refn.fs_id);
189274dcf55SMike Marshall 
1908bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_REMOVE);
191274dcf55SMike Marshall 	if (!new_op)
192274dcf55SMike Marshall 		return -ENOMEM;
193274dcf55SMike Marshall 
194274dcf55SMike Marshall 	new_op->upcall.req.remove.parent_refn = parent->refn;
195274dcf55SMike Marshall 	strncpy(new_op->upcall.req.remove.d_name, dentry->d_name.name,
1966bdfb48dSXiongfeng Wang 		ORANGEFS_NAME_MAX - 1);
197274dcf55SMike Marshall 
1988bb8aefdSYi Liu 	ret = service_operation(new_op, "orangefs_unlink",
199274dcf55SMike Marshall 				get_interruptible_flag(inode));
200274dcf55SMike Marshall 
2015253487eSMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
2025253487eSMike Marshall 		     "%s: service_operation returned:%d:\n",
2035253487eSMike Marshall 		     __func__,
2045253487eSMike Marshall 		     ret);
2055253487eSMike Marshall 
206274dcf55SMike Marshall 	op_release(new_op);
207274dcf55SMike Marshall 
208274dcf55SMike Marshall 	if (!ret) {
209274dcf55SMike Marshall 		drop_nlink(inode);
210274dcf55SMike Marshall 
211c2050a45SDeepa Dinamani 		dir->i_mtime = dir->i_ctime = current_time(dir);
212a55f2d86SMartin Brandenburg 		memset(&iattr, 0, sizeof iattr);
213a55f2d86SMartin Brandenburg 		iattr.ia_valid |= ATTR_MTIME;
214a55f2d86SMartin Brandenburg 		orangefs_inode_setattr(dir, &iattr);
215274dcf55SMike Marshall 		mark_inode_dirty_sync(dir);
216274dcf55SMike Marshall 	}
217274dcf55SMike Marshall 	return ret;
218274dcf55SMike Marshall }
219274dcf55SMike Marshall 
2208bb8aefdSYi Liu static int orangefs_symlink(struct inode *dir,
221274dcf55SMike Marshall 			 struct dentry *dentry,
222274dcf55SMike Marshall 			 const char *symname)
223274dcf55SMike Marshall {
2248bb8aefdSYi Liu 	struct orangefs_inode_s *parent = ORANGEFS_I(dir);
2258bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op;
226db0267e7SMartin Brandenburg 	struct orangefs_object_kref ref;
227274dcf55SMike Marshall 	struct inode *inode;
228a55f2d86SMartin Brandenburg 	struct iattr iattr;
229274dcf55SMike Marshall 	int mode = 755;
230274dcf55SMike Marshall 	int ret;
231274dcf55SMike Marshall 
232274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG, "%s: called\n", __func__);
233274dcf55SMike Marshall 
234274dcf55SMike Marshall 	if (!symname)
235274dcf55SMike Marshall 		return -EINVAL;
236274dcf55SMike Marshall 
237c62da585SMartin Brandenburg 	if (strlen(symname)+1 > ORANGEFS_NAME_MAX)
238c62da585SMartin Brandenburg 		return -ENAMETOOLONG;
239c62da585SMartin Brandenburg 
2408bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_SYMLINK);
241274dcf55SMike Marshall 	if (!new_op)
242274dcf55SMike Marshall 		return -ENOMEM;
243274dcf55SMike Marshall 
244274dcf55SMike Marshall 	new_op->upcall.req.sym.parent_refn = parent->refn;
245274dcf55SMike Marshall 
246274dcf55SMike Marshall 	fill_default_sys_attrs(new_op->upcall.req.sym.attributes,
2478bb8aefdSYi Liu 			       ORANGEFS_TYPE_SYMLINK,
248274dcf55SMike Marshall 			       mode);
249274dcf55SMike Marshall 
250274dcf55SMike Marshall 	strncpy(new_op->upcall.req.sym.entry_name,
251274dcf55SMike Marshall 		dentry->d_name.name,
2526bdfb48dSXiongfeng Wang 		ORANGEFS_NAME_MAX - 1);
2536bdfb48dSXiongfeng Wang 	strncpy(new_op->upcall.req.sym.target, symname, ORANGEFS_NAME_MAX - 1);
254274dcf55SMike Marshall 
255274dcf55SMike Marshall 	ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
256274dcf55SMike Marshall 
257274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
2588bb8aefdSYi Liu 		     "Symlink Got ORANGEFS handle %pU on fsid %d (ret=%d)\n",
259274dcf55SMike Marshall 		     &new_op->downcall.resp.sym.refn.khandle,
260274dcf55SMike Marshall 		     new_op->downcall.resp.sym.refn.fs_id, ret);
261274dcf55SMike Marshall 
262274dcf55SMike Marshall 	if (ret < 0) {
263274dcf55SMike Marshall 		gossip_debug(GOSSIP_NAME_DEBUG,
264274dcf55SMike Marshall 			    "%s: failed with error code %d\n",
265274dcf55SMike Marshall 			    __func__, ret);
266274dcf55SMike Marshall 		goto out;
267274dcf55SMike Marshall 	}
268274dcf55SMike Marshall 
269db0267e7SMartin Brandenburg 	ref = new_op->downcall.resp.sym.refn;
270db0267e7SMartin Brandenburg 
271db0267e7SMartin Brandenburg 	inode = orangefs_new_inode(dir->i_sb, dir, S_IFLNK | mode, 0, &ref);
272274dcf55SMike Marshall 	if (IS_ERR(inode)) {
273274dcf55SMike Marshall 		gossip_err
2748bb8aefdSYi Liu 		    ("*** Failed to allocate orangefs symlink inode\n");
275274dcf55SMike Marshall 		ret = PTR_ERR(inode);
276274dcf55SMike Marshall 		goto out;
277274dcf55SMike Marshall 	}
278f6a4b4c9SMartin Brandenburg 	/*
279f6a4b4c9SMartin Brandenburg 	 * This is necessary because orangefs_inode_getattr will not
280f6a4b4c9SMartin Brandenburg 	 * re-read symlink size as it is impossible for it to change.
281f6a4b4c9SMartin Brandenburg 	 * Invalidating the cache does not help.  orangefs_new_inode
282f6a4b4c9SMartin Brandenburg 	 * does not set the correct size (it does not know symname).
283f6a4b4c9SMartin Brandenburg 	 */
284f6a4b4c9SMartin Brandenburg 	inode->i_size = strlen(symname);
285274dcf55SMike Marshall 
286274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
287274dcf55SMike Marshall 		     "Assigned symlink inode new number of %pU\n",
288274dcf55SMike Marshall 		     get_khandle_from_ino(inode));
289274dcf55SMike Marshall 
2901e2e547aSAl Viro 	d_instantiate_new(dentry, inode);
291804b1737SMiklos Szeredi 	orangefs_set_timeout(dentry);
292274dcf55SMike Marshall 
293274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
294f66debf1SAl Viro 		     "Inode (Symlink) %pU -> %pd\n",
295274dcf55SMike Marshall 		     get_khandle_from_ino(inode),
296f66debf1SAl Viro 		     dentry);
297274dcf55SMike Marshall 
298c2050a45SDeepa Dinamani 	dir->i_mtime = dir->i_ctime = current_time(dir);
299a55f2d86SMartin Brandenburg 	memset(&iattr, 0, sizeof iattr);
300a55f2d86SMartin Brandenburg 	iattr.ia_valid |= ATTR_MTIME;
301a55f2d86SMartin Brandenburg 	orangefs_inode_setattr(dir, &iattr);
302274dcf55SMike Marshall 	mark_inode_dirty_sync(dir);
303274dcf55SMike Marshall 	ret = 0;
304274dcf55SMike Marshall out:
30534e6148aSMike Marshall 	op_release(new_op);
306274dcf55SMike Marshall 	return ret;
307274dcf55SMike Marshall }
308274dcf55SMike Marshall 
3098bb8aefdSYi Liu static int orangefs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
310274dcf55SMike Marshall {
3118bb8aefdSYi Liu 	struct orangefs_inode_s *parent = ORANGEFS_I(dir);
3128bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op;
313db0267e7SMartin Brandenburg 	struct orangefs_object_kref ref;
314274dcf55SMike Marshall 	struct inode *inode;
315a55f2d86SMartin Brandenburg 	struct iattr iattr;
316274dcf55SMike Marshall 	int ret;
317274dcf55SMike Marshall 
3188bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_MKDIR);
319274dcf55SMike Marshall 	if (!new_op)
320274dcf55SMike Marshall 		return -ENOMEM;
321274dcf55SMike Marshall 
322274dcf55SMike Marshall 	new_op->upcall.req.mkdir.parent_refn = parent->refn;
323274dcf55SMike Marshall 
324274dcf55SMike Marshall 	fill_default_sys_attrs(new_op->upcall.req.mkdir.attributes,
3258bb8aefdSYi Liu 			      ORANGEFS_TYPE_DIRECTORY, mode);
326274dcf55SMike Marshall 
327274dcf55SMike Marshall 	strncpy(new_op->upcall.req.mkdir.d_name,
3286bdfb48dSXiongfeng Wang 		dentry->d_name.name, ORANGEFS_NAME_MAX - 1);
329274dcf55SMike Marshall 
330274dcf55SMike Marshall 	ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
331274dcf55SMike Marshall 
332274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
3338bb8aefdSYi Liu 		     "Mkdir Got ORANGEFS handle %pU on fsid %d\n",
334274dcf55SMike Marshall 		     &new_op->downcall.resp.mkdir.refn.khandle,
335274dcf55SMike Marshall 		     new_op->downcall.resp.mkdir.refn.fs_id);
336274dcf55SMike Marshall 
337274dcf55SMike Marshall 	if (ret < 0) {
338274dcf55SMike Marshall 		gossip_debug(GOSSIP_NAME_DEBUG,
339274dcf55SMike Marshall 			     "%s: failed with error code %d\n",
340274dcf55SMike Marshall 			     __func__, ret);
341274dcf55SMike Marshall 		goto out;
342274dcf55SMike Marshall 	}
343274dcf55SMike Marshall 
344db0267e7SMartin Brandenburg 	ref = new_op->downcall.resp.mkdir.refn;
345db0267e7SMartin Brandenburg 
346db0267e7SMartin Brandenburg 	inode = orangefs_new_inode(dir->i_sb, dir, S_IFDIR | mode, 0, &ref);
347274dcf55SMike Marshall 	if (IS_ERR(inode)) {
3488bb8aefdSYi Liu 		gossip_err("*** Failed to allocate orangefs dir inode\n");
349274dcf55SMike Marshall 		ret = PTR_ERR(inode);
350274dcf55SMike Marshall 		goto out;
351274dcf55SMike Marshall 	}
352274dcf55SMike Marshall 
353274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
354274dcf55SMike Marshall 		     "Assigned dir inode new number of %pU\n",
355274dcf55SMike Marshall 		     get_khandle_from_ino(inode));
356274dcf55SMike Marshall 
3571e2e547aSAl Viro 	d_instantiate_new(dentry, inode);
358804b1737SMiklos Szeredi 	orangefs_set_timeout(dentry);
359274dcf55SMike Marshall 
360274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
361f66debf1SAl Viro 		     "Inode (Directory) %pU -> %pd\n",
362274dcf55SMike Marshall 		     get_khandle_from_ino(inode),
363f66debf1SAl Viro 		     dentry);
364274dcf55SMike Marshall 
365274dcf55SMike Marshall 	/*
366274dcf55SMike Marshall 	 * NOTE: we have no good way to keep nlink consistent for directories
367274dcf55SMike Marshall 	 * across clients; keep constant at 1.
368274dcf55SMike Marshall 	 */
369c2050a45SDeepa Dinamani 	dir->i_mtime = dir->i_ctime = current_time(dir);
370a55f2d86SMartin Brandenburg 	memset(&iattr, 0, sizeof iattr);
371a55f2d86SMartin Brandenburg 	iattr.ia_valid |= ATTR_MTIME;
372a55f2d86SMartin Brandenburg 	orangefs_inode_setattr(dir, &iattr);
373274dcf55SMike Marshall 	mark_inode_dirty_sync(dir);
374274dcf55SMike Marshall out:
37534e6148aSMike Marshall 	op_release(new_op);
376274dcf55SMike Marshall 	return ret;
377274dcf55SMike Marshall }
378274dcf55SMike Marshall 
3798bb8aefdSYi Liu static int orangefs_rename(struct inode *old_dir,
380274dcf55SMike Marshall 			struct dentry *old_dentry,
381274dcf55SMike Marshall 			struct inode *new_dir,
3821cd66c93SMiklos Szeredi 			struct dentry *new_dentry,
3831cd66c93SMiklos Szeredi 			unsigned int flags)
384274dcf55SMike Marshall {
3858bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op;
386*5e7f1d43SMartin Brandenburg 	struct iattr iattr;
387274dcf55SMike Marshall 	int ret;
388274dcf55SMike Marshall 
3891cd66c93SMiklos Szeredi 	if (flags)
3901cd66c93SMiklos Szeredi 		return -EINVAL;
3911cd66c93SMiklos Szeredi 
392274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
39396b0cffbSAl Viro 		     "orangefs_rename: called (%pd2 => %pd2) ct=%d\n",
39496b0cffbSAl Viro 		     old_dentry, new_dentry, d_count(new_dentry));
395274dcf55SMike Marshall 
396*5e7f1d43SMartin Brandenburg 	new_dir->i_mtime = new_dir->i_ctime = current_time(new_dir);
397*5e7f1d43SMartin Brandenburg 	memset(&iattr, 0, sizeof iattr);
398*5e7f1d43SMartin Brandenburg 	iattr.ia_valid |= ATTR_MTIME;
399*5e7f1d43SMartin Brandenburg 	orangefs_inode_setattr(new_dir, &iattr);
400*5e7f1d43SMartin Brandenburg 	mark_inode_dirty_sync(new_dir);
40171680c18SMartin Brandenburg 
4028bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_RENAME);
403274dcf55SMike Marshall 	if (!new_op)
404274dcf55SMike Marshall 		return -EINVAL;
405274dcf55SMike Marshall 
4068bb8aefdSYi Liu 	new_op->upcall.req.rename.old_parent_refn = ORANGEFS_I(old_dir)->refn;
4078bb8aefdSYi Liu 	new_op->upcall.req.rename.new_parent_refn = ORANGEFS_I(new_dir)->refn;
408274dcf55SMike Marshall 
409274dcf55SMike Marshall 	strncpy(new_op->upcall.req.rename.d_old_name,
410274dcf55SMike Marshall 		old_dentry->d_name.name,
4116bdfb48dSXiongfeng Wang 		ORANGEFS_NAME_MAX - 1);
412274dcf55SMike Marshall 	strncpy(new_op->upcall.req.rename.d_new_name,
413274dcf55SMike Marshall 		new_dentry->d_name.name,
4146bdfb48dSXiongfeng Wang 		ORANGEFS_NAME_MAX - 1);
415274dcf55SMike Marshall 
416274dcf55SMike Marshall 	ret = service_operation(new_op,
4178bb8aefdSYi Liu 				"orangefs_rename",
418274dcf55SMike Marshall 				get_interruptible_flag(old_dentry->d_inode));
419274dcf55SMike Marshall 
420274dcf55SMike Marshall 	gossip_debug(GOSSIP_NAME_DEBUG,
4218bb8aefdSYi Liu 		     "orangefs_rename: got downcall status %d\n",
422274dcf55SMike Marshall 		     ret);
423274dcf55SMike Marshall 
424274dcf55SMike Marshall 	if (new_dentry->d_inode)
425c2050a45SDeepa Dinamani 		new_dentry->d_inode->i_ctime = current_time(new_dentry->d_inode);
426274dcf55SMike Marshall 
427274dcf55SMike Marshall 	op_release(new_op);
428274dcf55SMike Marshall 	return ret;
429274dcf55SMike Marshall }
430274dcf55SMike Marshall 
4318bb8aefdSYi Liu /* ORANGEFS implementation of VFS inode operations for directories */
4326f3fc107SAl Viro const struct inode_operations orangefs_dir_inode_operations = {
4338bb8aefdSYi Liu 	.lookup = orangefs_lookup,
4348bb8aefdSYi Liu 	.get_acl = orangefs_get_acl,
4358bb8aefdSYi Liu 	.set_acl = orangefs_set_acl,
4368bb8aefdSYi Liu 	.create = orangefs_create,
4378bb8aefdSYi Liu 	.unlink = orangefs_unlink,
4388bb8aefdSYi Liu 	.symlink = orangefs_symlink,
4398bb8aefdSYi Liu 	.mkdir = orangefs_mkdir,
4408bb8aefdSYi Liu 	.rmdir = orangefs_unlink,
4418bb8aefdSYi Liu 	.rename = orangefs_rename,
4428bb8aefdSYi Liu 	.setattr = orangefs_setattr,
4438bb8aefdSYi Liu 	.getattr = orangefs_getattr,
4448bb8aefdSYi Liu 	.listxattr = orangefs_listxattr,
445933287daSMartin Brandenburg 	.permission = orangefs_permission,
446a55f2d86SMartin Brandenburg 	.update_time = orangefs_update_time,
447274dcf55SMike Marshall };
448