blob: 9f82eb37785746f5f0e2b9c653678291b0bf53b0 [file] [log] [blame]
Mike Marshall274dcf52015-07-17 10:38:13 -04001/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7/*
8 * Linux VFS namei operations.
9 */
10
11#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -050012#include "orangefs-kernel.h"
Mike Marshall274dcf52015-07-17 10:38:13 -040013
14/*
15 * Get a newly allocated inode to go with a negative dentry.
16 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050017static int orangefs_create(struct inode *dir,
Mike Marshall274dcf52015-07-17 10:38:13 -040018 struct dentry *dentry,
19 umode_t mode,
20 bool exclusive)
21{
Yi Liu8bb8aef2015-11-24 15:12:14 -050022 struct orangefs_inode_s *parent = ORANGEFS_I(dir);
23 struct orangefs_kernel_op_s *new_op;
Mike Marshall274dcf52015-07-17 10:38:13 -040024 struct inode *inode;
25 int ret;
26
27 gossip_debug(GOSSIP_NAME_DEBUG, "%s: called\n", __func__);
28
Yi Liu8bb8aef2015-11-24 15:12:14 -050029 new_op = op_alloc(ORANGEFS_VFS_OP_CREATE);
Mike Marshall274dcf52015-07-17 10:38:13 -040030 if (!new_op)
31 return -ENOMEM;
32
33 new_op->upcall.req.create.parent_refn = parent->refn;
34
35 fill_default_sys_attrs(new_op->upcall.req.create.attributes,
Yi Liu8bb8aef2015-11-24 15:12:14 -050036 ORANGEFS_TYPE_METAFILE, mode);
Mike Marshall274dcf52015-07-17 10:38:13 -040037
38 strncpy(new_op->upcall.req.create.d_name,
Yi Liu8bb8aef2015-11-24 15:12:14 -050039 dentry->d_name.name, ORANGEFS_NAME_LEN);
Mike Marshall274dcf52015-07-17 10:38:13 -040040
41 ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
42
43 gossip_debug(GOSSIP_NAME_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -050044 "Create Got ORANGEFS handle %pU on fsid %d (ret=%d)\n",
Mike Marshall274dcf52015-07-17 10:38:13 -040045 &new_op->downcall.resp.create.refn.khandle,
46 new_op->downcall.resp.create.refn.fs_id, ret);
47
48 if (ret < 0) {
49 gossip_debug(GOSSIP_NAME_DEBUG,
50 "%s: failed with error code %d\n",
51 __func__, ret);
52 goto out;
53 }
54
Yi Liu8bb8aef2015-11-24 15:12:14 -050055 inode = orangefs_new_inode(dir->i_sb, dir, S_IFREG | mode, 0,
Mike Marshall274dcf52015-07-17 10:38:13 -040056 &new_op->downcall.resp.create.refn);
57 if (IS_ERR(inode)) {
Yi Liu8bb8aef2015-11-24 15:12:14 -050058 gossip_err("*** Failed to allocate orangefs file inode\n");
Mike Marshall274dcf52015-07-17 10:38:13 -040059 ret = PTR_ERR(inode);
60 goto out;
61 }
62
63 gossip_debug(GOSSIP_NAME_DEBUG,
64 "Assigned file inode new number of %pU\n",
65 get_khandle_from_ino(inode));
66
67 d_instantiate(dentry, inode);
68 unlock_new_inode(inode);
69
70 gossip_debug(GOSSIP_NAME_DEBUG,
71 "Inode (Regular File) %pU -> %s\n",
72 get_khandle_from_ino(inode),
73 dentry->d_name.name);
74
75 SetMtimeFlag(parent);
76 dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb);
77 mark_inode_dirty_sync(dir);
78 ret = 0;
79out:
80 op_release(new_op);
81 gossip_debug(GOSSIP_NAME_DEBUG, "%s: returning %d\n", __func__, ret);
82 return ret;
83}
84
85/*
86 * Attempt to resolve an object name (dentry->d_name), parent handle, and
87 * fsid into a handle for the object.
88 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050089static struct dentry *orangefs_lookup(struct inode *dir, struct dentry *dentry,
Mike Marshall274dcf52015-07-17 10:38:13 -040090 unsigned int flags)
91{
Yi Liu8bb8aef2015-11-24 15:12:14 -050092 struct orangefs_inode_s *parent = ORANGEFS_I(dir);
93 struct orangefs_kernel_op_s *new_op;
Mike Marshall274dcf52015-07-17 10:38:13 -040094 struct inode *inode;
95 struct dentry *res;
96 int ret = -EINVAL;
97
98 /*
99 * in theory we could skip a lookup here (if the intent is to
100 * create) in order to avoid a potentially failed lookup, but
101 * leaving it in can skip a valid lookup and try to create a file
102 * that already exists (e.g. the vfs already handles checking for
103 * -EEXIST on O_EXCL opens, which is broken if we skip this lookup
104 * in the create path)
105 */
106 gossip_debug(GOSSIP_NAME_DEBUG, "%s called on %s\n",
107 __func__, dentry->d_name.name);
108
Yi Liu8bb8aef2015-11-24 15:12:14 -0500109 if (dentry->d_name.len > (ORANGEFS_NAME_LEN - 1))
Mike Marshall274dcf52015-07-17 10:38:13 -0400110 return ERR_PTR(-ENAMETOOLONG);
111
Yi Liu8bb8aef2015-11-24 15:12:14 -0500112 new_op = op_alloc(ORANGEFS_VFS_OP_LOOKUP);
Mike Marshall274dcf52015-07-17 10:38:13 -0400113 if (!new_op)
114 return ERR_PTR(-ENOMEM);
115
Mike Marshalle91f1c82015-12-04 13:06:55 -0500116 new_op->upcall.req.lookup.sym_follow = PVFS2_LOOKUP_LINK_NO_FOLLOW;
Mike Marshall274dcf52015-07-17 10:38:13 -0400117
118 gossip_debug(GOSSIP_NAME_DEBUG, "%s:%s:%d using parent %pU\n",
119 __FILE__,
120 __func__,
121 __LINE__,
122 &parent->refn.khandle);
123 new_op->upcall.req.lookup.parent_refn = parent->refn;
124
125 strncpy(new_op->upcall.req.lookup.d_name, dentry->d_name.name,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500126 ORANGEFS_NAME_LEN);
Mike Marshall274dcf52015-07-17 10:38:13 -0400127
128 gossip_debug(GOSSIP_NAME_DEBUG,
129 "%s: doing lookup on %s under %pU,%d (follow=%s)\n",
130 __func__,
131 new_op->upcall.req.lookup.d_name,
132 &new_op->upcall.req.lookup.parent_refn.khandle,
133 new_op->upcall.req.lookup.parent_refn.fs_id,
134 ((new_op->upcall.req.lookup.sym_follow ==
Yi Liu8bb8aef2015-11-24 15:12:14 -0500135 ORANGEFS_LOOKUP_LINK_FOLLOW) ? "yes" : "no"));
Mike Marshall274dcf52015-07-17 10:38:13 -0400136
137 ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
138
139 gossip_debug(GOSSIP_NAME_DEBUG,
140 "Lookup Got %pU, fsid %d (ret=%d)\n",
141 &new_op->downcall.resp.lookup.refn.khandle,
142 new_op->downcall.resp.lookup.refn.fs_id,
143 ret);
144
145 if (ret < 0) {
146 if (ret == -ENOENT) {
147 /*
148 * if no inode was found, add a negative dentry to
149 * dcache anyway; if we don't, we don't hold expected
150 * lookup semantics and we most noticeably break
151 * during directory renames.
152 *
153 * however, if the operation failed or exited, do not
154 * add the dentry (e.g. in the case that a touch is
155 * issued on a file that already exists that was
156 * interrupted during this lookup -- no need to add
157 * another negative dentry for an existing file)
158 */
159
160 gossip_debug(GOSSIP_NAME_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500161 "orangefs_lookup: Adding *negative* dentry "
Mike Marshall274dcf52015-07-17 10:38:13 -0400162 "%p for %s\n",
163 dentry,
164 dentry->d_name.name);
165
166 d_add(dentry, NULL);
167 res = NULL;
168 goto out;
169 }
170
171 /* must be a non-recoverable error */
172 res = ERR_PTR(ret);
173 goto out;
174 }
175
Yi Liu8bb8aef2015-11-24 15:12:14 -0500176 inode = orangefs_iget(dir->i_sb, &new_op->downcall.resp.lookup.refn);
Mike Marshall274dcf52015-07-17 10:38:13 -0400177 if (IS_ERR(inode)) {
178 gossip_debug(GOSSIP_NAME_DEBUG,
179 "error %ld from iget\n", PTR_ERR(inode));
180 res = ERR_CAST(inode);
181 goto out;
182 }
183
184 gossip_debug(GOSSIP_NAME_DEBUG,
185 "%s:%s:%d "
186 "Found good inode [%lu] with count [%d]\n",
187 __FILE__,
188 __func__,
189 __LINE__,
190 inode->i_ino,
191 (int)atomic_read(&inode->i_count));
192
193 /* update dentry/inode pair into dcache */
194 res = d_splice_alias(inode, dentry);
195
196 gossip_debug(GOSSIP_NAME_DEBUG,
197 "Lookup success (inode ct = %d)\n",
198 (int)atomic_read(&inode->i_count));
199out:
200 op_release(new_op);
201 return res;
202}
203
204/* return 0 on success; non-zero otherwise */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500205static int orangefs_unlink(struct inode *dir, struct dentry *dentry)
Mike Marshall274dcf52015-07-17 10:38:13 -0400206{
207 struct inode *inode = dentry->d_inode;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500208 struct orangefs_inode_s *parent = ORANGEFS_I(dir);
209 struct orangefs_kernel_op_s *new_op;
Mike Marshall274dcf52015-07-17 10:38:13 -0400210 int ret;
211
212 gossip_debug(GOSSIP_NAME_DEBUG,
213 "%s: called on %s\n"
214 " (inode %pU): Parent is %pU | fs_id %d\n",
215 __func__,
216 dentry->d_name.name,
217 get_khandle_from_ino(inode),
218 &parent->refn.khandle,
219 parent->refn.fs_id);
220
Yi Liu8bb8aef2015-11-24 15:12:14 -0500221 new_op = op_alloc(ORANGEFS_VFS_OP_REMOVE);
Mike Marshall274dcf52015-07-17 10:38:13 -0400222 if (!new_op)
223 return -ENOMEM;
224
225 new_op->upcall.req.remove.parent_refn = parent->refn;
226 strncpy(new_op->upcall.req.remove.d_name, dentry->d_name.name,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500227 ORANGEFS_NAME_LEN);
Mike Marshall274dcf52015-07-17 10:38:13 -0400228
Yi Liu8bb8aef2015-11-24 15:12:14 -0500229 ret = service_operation(new_op, "orangefs_unlink",
Mike Marshall274dcf52015-07-17 10:38:13 -0400230 get_interruptible_flag(inode));
231
232 /* when request is serviced properly, free req op struct */
233 op_release(new_op);
234
235 if (!ret) {
236 drop_nlink(inode);
237
238 SetMtimeFlag(parent);
239 dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb);
240 mark_inode_dirty_sync(dir);
241 }
242 return ret;
243}
244
Yi Liu8bb8aef2015-11-24 15:12:14 -0500245static int orangefs_symlink(struct inode *dir,
Mike Marshall274dcf52015-07-17 10:38:13 -0400246 struct dentry *dentry,
247 const char *symname)
248{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500249 struct orangefs_inode_s *parent = ORANGEFS_I(dir);
250 struct orangefs_kernel_op_s *new_op;
Mike Marshall274dcf52015-07-17 10:38:13 -0400251 struct inode *inode;
252 int mode = 755;
253 int ret;
254
255 gossip_debug(GOSSIP_NAME_DEBUG, "%s: called\n", __func__);
256
257 if (!symname)
258 return -EINVAL;
259
Yi Liu8bb8aef2015-11-24 15:12:14 -0500260 new_op = op_alloc(ORANGEFS_VFS_OP_SYMLINK);
Mike Marshall274dcf52015-07-17 10:38:13 -0400261 if (!new_op)
262 return -ENOMEM;
263
264 new_op->upcall.req.sym.parent_refn = parent->refn;
265
266 fill_default_sys_attrs(new_op->upcall.req.sym.attributes,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500267 ORANGEFS_TYPE_SYMLINK,
Mike Marshall274dcf52015-07-17 10:38:13 -0400268 mode);
269
270 strncpy(new_op->upcall.req.sym.entry_name,
271 dentry->d_name.name,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500272 ORANGEFS_NAME_LEN);
273 strncpy(new_op->upcall.req.sym.target, symname, ORANGEFS_NAME_LEN);
Mike Marshall274dcf52015-07-17 10:38:13 -0400274
275 ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
276
277 gossip_debug(GOSSIP_NAME_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500278 "Symlink Got ORANGEFS handle %pU on fsid %d (ret=%d)\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400279 &new_op->downcall.resp.sym.refn.khandle,
280 new_op->downcall.resp.sym.refn.fs_id, ret);
281
282 if (ret < 0) {
283 gossip_debug(GOSSIP_NAME_DEBUG,
284 "%s: failed with error code %d\n",
285 __func__, ret);
286 goto out;
287 }
288
Yi Liu8bb8aef2015-11-24 15:12:14 -0500289 inode = orangefs_new_inode(dir->i_sb, dir, S_IFLNK | mode, 0,
Mike Marshall274dcf52015-07-17 10:38:13 -0400290 &new_op->downcall.resp.sym.refn);
291 if (IS_ERR(inode)) {
292 gossip_err
Yi Liu8bb8aef2015-11-24 15:12:14 -0500293 ("*** Failed to allocate orangefs symlink inode\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400294 ret = PTR_ERR(inode);
295 goto out;
296 }
297
298 gossip_debug(GOSSIP_NAME_DEBUG,
299 "Assigned symlink inode new number of %pU\n",
300 get_khandle_from_ino(inode));
301
302 d_instantiate(dentry, inode);
303 unlock_new_inode(inode);
304
305 gossip_debug(GOSSIP_NAME_DEBUG,
306 "Inode (Symlink) %pU -> %s\n",
307 get_khandle_from_ino(inode),
308 dentry->d_name.name);
309
310 SetMtimeFlag(parent);
311 dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb);
312 mark_inode_dirty_sync(dir);
313 ret = 0;
314out:
315 op_release(new_op);
316 return ret;
317}
318
Yi Liu8bb8aef2015-11-24 15:12:14 -0500319static int orangefs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Mike Marshall274dcf52015-07-17 10:38:13 -0400320{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500321 struct orangefs_inode_s *parent = ORANGEFS_I(dir);
322 struct orangefs_kernel_op_s *new_op;
Mike Marshall274dcf52015-07-17 10:38:13 -0400323 struct inode *inode;
324 int ret;
325
Yi Liu8bb8aef2015-11-24 15:12:14 -0500326 new_op = op_alloc(ORANGEFS_VFS_OP_MKDIR);
Mike Marshall274dcf52015-07-17 10:38:13 -0400327 if (!new_op)
328 return -ENOMEM;
329
330 new_op->upcall.req.mkdir.parent_refn = parent->refn;
331
332 fill_default_sys_attrs(new_op->upcall.req.mkdir.attributes,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500333 ORANGEFS_TYPE_DIRECTORY, mode);
Mike Marshall274dcf52015-07-17 10:38:13 -0400334
335 strncpy(new_op->upcall.req.mkdir.d_name,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500336 dentry->d_name.name, ORANGEFS_NAME_LEN);
Mike Marshall274dcf52015-07-17 10:38:13 -0400337
338 ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
339
340 gossip_debug(GOSSIP_NAME_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500341 "Mkdir Got ORANGEFS handle %pU on fsid %d\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400342 &new_op->downcall.resp.mkdir.refn.khandle,
343 new_op->downcall.resp.mkdir.refn.fs_id);
344
345 if (ret < 0) {
346 gossip_debug(GOSSIP_NAME_DEBUG,
347 "%s: failed with error code %d\n",
348 __func__, ret);
349 goto out;
350 }
351
Yi Liu8bb8aef2015-11-24 15:12:14 -0500352 inode = orangefs_new_inode(dir->i_sb, dir, S_IFDIR | mode, 0,
Mike Marshall274dcf52015-07-17 10:38:13 -0400353 &new_op->downcall.resp.mkdir.refn);
354 if (IS_ERR(inode)) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500355 gossip_err("*** Failed to allocate orangefs dir inode\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400356 ret = PTR_ERR(inode);
357 goto out;
358 }
359
360 gossip_debug(GOSSIP_NAME_DEBUG,
361 "Assigned dir inode new number of %pU\n",
362 get_khandle_from_ino(inode));
363
364 d_instantiate(dentry, inode);
365 unlock_new_inode(inode);
366
367 gossip_debug(GOSSIP_NAME_DEBUG,
368 "Inode (Directory) %pU -> %s\n",
369 get_khandle_from_ino(inode),
370 dentry->d_name.name);
371
372 /*
373 * NOTE: we have no good way to keep nlink consistent for directories
374 * across clients; keep constant at 1.
375 */
376 SetMtimeFlag(parent);
377 dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb);
378 mark_inode_dirty_sync(dir);
379out:
380 op_release(new_op);
381 return ret;
382}
383
Yi Liu8bb8aef2015-11-24 15:12:14 -0500384static int orangefs_rename(struct inode *old_dir,
Mike Marshall274dcf52015-07-17 10:38:13 -0400385 struct dentry *old_dentry,
386 struct inode *new_dir,
387 struct dentry *new_dentry)
388{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500389 struct orangefs_kernel_op_s *new_op;
Mike Marshall274dcf52015-07-17 10:38:13 -0400390 int ret;
391
392 gossip_debug(GOSSIP_NAME_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500393 "orangefs_rename: called (%s/%s => %s/%s) ct=%d\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400394 old_dentry->d_parent->d_name.name,
395 old_dentry->d_name.name,
396 new_dentry->d_parent->d_name.name,
397 new_dentry->d_name.name,
398 d_count(new_dentry));
399
Yi Liu8bb8aef2015-11-24 15:12:14 -0500400 new_op = op_alloc(ORANGEFS_VFS_OP_RENAME);
Mike Marshall274dcf52015-07-17 10:38:13 -0400401 if (!new_op)
402 return -EINVAL;
403
Yi Liu8bb8aef2015-11-24 15:12:14 -0500404 new_op->upcall.req.rename.old_parent_refn = ORANGEFS_I(old_dir)->refn;
405 new_op->upcall.req.rename.new_parent_refn = ORANGEFS_I(new_dir)->refn;
Mike Marshall274dcf52015-07-17 10:38:13 -0400406
407 strncpy(new_op->upcall.req.rename.d_old_name,
408 old_dentry->d_name.name,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500409 ORANGEFS_NAME_LEN);
Mike Marshall274dcf52015-07-17 10:38:13 -0400410 strncpy(new_op->upcall.req.rename.d_new_name,
411 new_dentry->d_name.name,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500412 ORANGEFS_NAME_LEN);
Mike Marshall274dcf52015-07-17 10:38:13 -0400413
414 ret = service_operation(new_op,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500415 "orangefs_rename",
Mike Marshall274dcf52015-07-17 10:38:13 -0400416 get_interruptible_flag(old_dentry->d_inode));
417
418 gossip_debug(GOSSIP_NAME_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500419 "orangefs_rename: got downcall status %d\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400420 ret);
421
422 if (new_dentry->d_inode)
423 new_dentry->d_inode->i_ctime = CURRENT_TIME;
424
425 op_release(new_op);
426 return ret;
427}
428
Yi Liu8bb8aef2015-11-24 15:12:14 -0500429/* ORANGEFS implementation of VFS inode operations for directories */
430struct inode_operations orangefs_dir_inode_operations = {
431 .lookup = orangefs_lookup,
432 .get_acl = orangefs_get_acl,
433 .set_acl = orangefs_set_acl,
434 .create = orangefs_create,
435 .unlink = orangefs_unlink,
436 .symlink = orangefs_symlink,
437 .mkdir = orangefs_mkdir,
438 .rmdir = orangefs_unlink,
439 .rename = orangefs_rename,
440 .setattr = orangefs_setattr,
441 .getattr = orangefs_getattr,
Mike Marshall274dcf52015-07-17 10:38:13 -0400442 .setxattr = generic_setxattr,
443 .getxattr = generic_getxattr,
444 .removexattr = generic_removexattr,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500445 .listxattr = orangefs_listxattr,
Mike Marshall274dcf52015-07-17 10:38:13 -0400446};