namei.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) 2001 Clemson University and The University of Chicago
  4. *
  5. * See COPYING in top-level directory.
  6. */
  7. /*
  8. * Linux VFS namei operations.
  9. */
  10. #include "protocol.h"
  11. #include "orangefs-kernel.h"
  12. /*
  13. * Get a newly allocated inode to go with a negative dentry.
  14. */
  15. static int orangefs_create(struct user_namespace *mnt_userns,
  16. struct inode *dir,
  17. struct dentry *dentry,
  18. umode_t mode,
  19. bool exclusive)
  20. {
  21. struct orangefs_inode_s *parent = ORANGEFS_I(dir);
  22. struct orangefs_kernel_op_s *new_op;
  23. struct orangefs_object_kref ref;
  24. struct inode *inode;
  25. struct iattr iattr;
  26. int ret;
  27. gossip_debug(GOSSIP_NAME_DEBUG, "%s: %pd\n",
  28. __func__,
  29. dentry);
  30. new_op = op_alloc(ORANGEFS_VFS_OP_CREATE);
  31. if (!new_op)
  32. return -ENOMEM;
  33. new_op->upcall.req.create.parent_refn = parent->refn;
  34. fill_default_sys_attrs(new_op->upcall.req.create.attributes,
  35. ORANGEFS_TYPE_METAFILE, mode);
  36. strncpy(new_op->upcall.req.create.d_name,
  37. dentry->d_name.name, ORANGEFS_NAME_MAX - 1);
  38. ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
  39. gossip_debug(GOSSIP_NAME_DEBUG,
  40. "%s: %pd: handle:%pU: fsid:%d: new_op:%p: ret:%d:\n",
  41. __func__,
  42. dentry,
  43. &new_op->downcall.resp.create.refn.khandle,
  44. new_op->downcall.resp.create.refn.fs_id,
  45. new_op,
  46. ret);
  47. if (ret < 0)
  48. goto out;
  49. ref = new_op->downcall.resp.create.refn;
  50. inode = orangefs_new_inode(dir->i_sb, dir, S_IFREG | mode, 0, &ref);
  51. if (IS_ERR(inode)) {
  52. gossip_err("%s: Failed to allocate inode for file :%pd:\n",
  53. __func__,
  54. dentry);
  55. ret = PTR_ERR(inode);
  56. goto out;
  57. }
  58. gossip_debug(GOSSIP_NAME_DEBUG,
  59. "%s: Assigned inode :%pU: for file :%pd:\n",
  60. __func__,
  61. get_khandle_from_ino(inode),
  62. dentry);
  63. d_instantiate_new(dentry, inode);
  64. orangefs_set_timeout(dentry);
  65. gossip_debug(GOSSIP_NAME_DEBUG,
  66. "%s: dentry instantiated for %pd\n",
  67. __func__,
  68. dentry);
  69. memset(&iattr, 0, sizeof iattr);
  70. iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME;
  71. iattr.ia_mtime = iattr.ia_ctime = current_time(dir);
  72. __orangefs_setattr(dir, &iattr);
  73. ret = 0;
  74. out:
  75. op_release(new_op);
  76. gossip_debug(GOSSIP_NAME_DEBUG,
  77. "%s: %pd: returning %d\n",
  78. __func__,
  79. dentry,
  80. ret);
  81. return ret;
  82. }
  83. /*
  84. * Attempt to resolve an object name (dentry->d_name), parent handle, and
  85. * fsid into a handle for the object.
  86. */
  87. static struct dentry *orangefs_lookup(struct inode *dir, struct dentry *dentry,
  88. unsigned int flags)
  89. {
  90. struct orangefs_inode_s *parent = ORANGEFS_I(dir);
  91. struct orangefs_kernel_op_s *new_op;
  92. struct inode *inode;
  93. int ret = -EINVAL;
  94. /*
  95. * in theory we could skip a lookup here (if the intent is to
  96. * create) in order to avoid a potentially failed lookup, but
  97. * leaving it in can skip a valid lookup and try to create a file
  98. * that already exists (e.g. the vfs already handles checking for
  99. * -EEXIST on O_EXCL opens, which is broken if we skip this lookup
  100. * in the create path)
  101. */
  102. gossip_debug(GOSSIP_NAME_DEBUG, "%s called on %pd\n",
  103. __func__, dentry);
  104. if (dentry->d_name.len > (ORANGEFS_NAME_MAX - 1))
  105. return ERR_PTR(-ENAMETOOLONG);
  106. new_op = op_alloc(ORANGEFS_VFS_OP_LOOKUP);
  107. if (!new_op)
  108. return ERR_PTR(-ENOMEM);
  109. new_op->upcall.req.lookup.sym_follow = ORANGEFS_LOOKUP_LINK_NO_FOLLOW;
  110. gossip_debug(GOSSIP_NAME_DEBUG, "%s:%s:%d using parent %pU\n",
  111. __FILE__,
  112. __func__,
  113. __LINE__,
  114. &parent->refn.khandle);
  115. new_op->upcall.req.lookup.parent_refn = parent->refn;
  116. strncpy(new_op->upcall.req.lookup.d_name, dentry->d_name.name,
  117. ORANGEFS_NAME_MAX - 1);
  118. gossip_debug(GOSSIP_NAME_DEBUG,
  119. "%s: doing lookup on %s under %pU,%d\n",
  120. __func__,
  121. new_op->upcall.req.lookup.d_name,
  122. &new_op->upcall.req.lookup.parent_refn.khandle,
  123. new_op->upcall.req.lookup.parent_refn.fs_id);
  124. ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
  125. gossip_debug(GOSSIP_NAME_DEBUG,
  126. "Lookup Got %pU, fsid %d (ret=%d)\n",
  127. &new_op->downcall.resp.lookup.refn.khandle,
  128. new_op->downcall.resp.lookup.refn.fs_id,
  129. ret);
  130. if (ret == 0) {
  131. orangefs_set_timeout(dentry);
  132. inode = orangefs_iget(dir->i_sb, &new_op->downcall.resp.lookup.refn);
  133. } else if (ret == -ENOENT) {
  134. inode = NULL;
  135. } else {
  136. /* must be a non-recoverable error */
  137. inode = ERR_PTR(ret);
  138. }
  139. op_release(new_op);
  140. return d_splice_alias(inode, dentry);
  141. }
  142. /* return 0 on success; non-zero otherwise */
  143. static int orangefs_unlink(struct inode *dir, struct dentry *dentry)
  144. {
  145. struct inode *inode = dentry->d_inode;
  146. struct orangefs_inode_s *parent = ORANGEFS_I(dir);
  147. struct orangefs_kernel_op_s *new_op;
  148. struct iattr iattr;
  149. int ret;
  150. gossip_debug(GOSSIP_NAME_DEBUG,
  151. "%s: called on %pd\n"
  152. " (inode %pU): Parent is %pU | fs_id %d\n",
  153. __func__,
  154. dentry,
  155. get_khandle_from_ino(inode),
  156. &parent->refn.khandle,
  157. parent->refn.fs_id);
  158. new_op = op_alloc(ORANGEFS_VFS_OP_REMOVE);
  159. if (!new_op)
  160. return -ENOMEM;
  161. new_op->upcall.req.remove.parent_refn = parent->refn;
  162. strncpy(new_op->upcall.req.remove.d_name, dentry->d_name.name,
  163. ORANGEFS_NAME_MAX - 1);
  164. ret = service_operation(new_op, "orangefs_unlink",
  165. get_interruptible_flag(inode));
  166. gossip_debug(GOSSIP_NAME_DEBUG,
  167. "%s: service_operation returned:%d:\n",
  168. __func__,
  169. ret);
  170. op_release(new_op);
  171. if (!ret) {
  172. drop_nlink(inode);
  173. memset(&iattr, 0, sizeof iattr);
  174. iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME;
  175. iattr.ia_mtime = iattr.ia_ctime = current_time(dir);
  176. __orangefs_setattr(dir, &iattr);
  177. }
  178. return ret;
  179. }
  180. static int orangefs_symlink(struct user_namespace *mnt_userns,
  181. struct inode *dir,
  182. struct dentry *dentry,
  183. const char *symname)
  184. {
  185. struct orangefs_inode_s *parent = ORANGEFS_I(dir);
  186. struct orangefs_kernel_op_s *new_op;
  187. struct orangefs_object_kref ref;
  188. struct inode *inode;
  189. struct iattr iattr;
  190. int mode = 0755;
  191. int ret;
  192. gossip_debug(GOSSIP_NAME_DEBUG, "%s: called\n", __func__);
  193. if (!symname)
  194. return -EINVAL;
  195. if (strlen(symname)+1 > ORANGEFS_NAME_MAX)
  196. return -ENAMETOOLONG;
  197. new_op = op_alloc(ORANGEFS_VFS_OP_SYMLINK);
  198. if (!new_op)
  199. return -ENOMEM;
  200. new_op->upcall.req.sym.parent_refn = parent->refn;
  201. fill_default_sys_attrs(new_op->upcall.req.sym.attributes,
  202. ORANGEFS_TYPE_SYMLINK,
  203. mode);
  204. strncpy(new_op->upcall.req.sym.entry_name,
  205. dentry->d_name.name,
  206. ORANGEFS_NAME_MAX - 1);
  207. strncpy(new_op->upcall.req.sym.target, symname, ORANGEFS_NAME_MAX - 1);
  208. ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
  209. gossip_debug(GOSSIP_NAME_DEBUG,
  210. "Symlink Got ORANGEFS handle %pU on fsid %d (ret=%d)\n",
  211. &new_op->downcall.resp.sym.refn.khandle,
  212. new_op->downcall.resp.sym.refn.fs_id, ret);
  213. if (ret < 0) {
  214. gossip_debug(GOSSIP_NAME_DEBUG,
  215. "%s: failed with error code %d\n",
  216. __func__, ret);
  217. goto out;
  218. }
  219. ref = new_op->downcall.resp.sym.refn;
  220. inode = orangefs_new_inode(dir->i_sb, dir, S_IFLNK | mode, 0, &ref);
  221. if (IS_ERR(inode)) {
  222. gossip_err
  223. ("*** Failed to allocate orangefs symlink inode\n");
  224. ret = PTR_ERR(inode);
  225. goto out;
  226. }
  227. /*
  228. * This is necessary because orangefs_inode_getattr will not
  229. * re-read symlink size as it is impossible for it to change.
  230. * Invalidating the cache does not help. orangefs_new_inode
  231. * does not set the correct size (it does not know symname).
  232. */
  233. inode->i_size = strlen(symname);
  234. gossip_debug(GOSSIP_NAME_DEBUG,
  235. "Assigned symlink inode new number of %pU\n",
  236. get_khandle_from_ino(inode));
  237. d_instantiate_new(dentry, inode);
  238. orangefs_set_timeout(dentry);
  239. gossip_debug(GOSSIP_NAME_DEBUG,
  240. "Inode (Symlink) %pU -> %pd\n",
  241. get_khandle_from_ino(inode),
  242. dentry);
  243. memset(&iattr, 0, sizeof iattr);
  244. iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME;
  245. iattr.ia_mtime = iattr.ia_ctime = current_time(dir);
  246. __orangefs_setattr(dir, &iattr);
  247. ret = 0;
  248. out:
  249. op_release(new_op);
  250. return ret;
  251. }
  252. static int orangefs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
  253. struct dentry *dentry, umode_t mode)
  254. {
  255. struct orangefs_inode_s *parent = ORANGEFS_I(dir);
  256. struct orangefs_kernel_op_s *new_op;
  257. struct orangefs_object_kref ref;
  258. struct inode *inode;
  259. struct iattr iattr;
  260. int ret;
  261. new_op = op_alloc(ORANGEFS_VFS_OP_MKDIR);
  262. if (!new_op)
  263. return -ENOMEM;
  264. new_op->upcall.req.mkdir.parent_refn = parent->refn;
  265. fill_default_sys_attrs(new_op->upcall.req.mkdir.attributes,
  266. ORANGEFS_TYPE_DIRECTORY, mode);
  267. strncpy(new_op->upcall.req.mkdir.d_name,
  268. dentry->d_name.name, ORANGEFS_NAME_MAX - 1);
  269. ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
  270. gossip_debug(GOSSIP_NAME_DEBUG,
  271. "Mkdir Got ORANGEFS handle %pU on fsid %d\n",
  272. &new_op->downcall.resp.mkdir.refn.khandle,
  273. new_op->downcall.resp.mkdir.refn.fs_id);
  274. if (ret < 0) {
  275. gossip_debug(GOSSIP_NAME_DEBUG,
  276. "%s: failed with error code %d\n",
  277. __func__, ret);
  278. goto out;
  279. }
  280. ref = new_op->downcall.resp.mkdir.refn;
  281. inode = orangefs_new_inode(dir->i_sb, dir, S_IFDIR | mode, 0, &ref);
  282. if (IS_ERR(inode)) {
  283. gossip_err("*** Failed to allocate orangefs dir inode\n");
  284. ret = PTR_ERR(inode);
  285. goto out;
  286. }
  287. gossip_debug(GOSSIP_NAME_DEBUG,
  288. "Assigned dir inode new number of %pU\n",
  289. get_khandle_from_ino(inode));
  290. d_instantiate_new(dentry, inode);
  291. orangefs_set_timeout(dentry);
  292. gossip_debug(GOSSIP_NAME_DEBUG,
  293. "Inode (Directory) %pU -> %pd\n",
  294. get_khandle_from_ino(inode),
  295. dentry);
  296. /*
  297. * NOTE: we have no good way to keep nlink consistent for directories
  298. * across clients; keep constant at 1.
  299. */
  300. memset(&iattr, 0, sizeof iattr);
  301. iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME;
  302. iattr.ia_mtime = iattr.ia_ctime = current_time(dir);
  303. __orangefs_setattr(dir, &iattr);
  304. out:
  305. op_release(new_op);
  306. return ret;
  307. }
  308. static int orangefs_rename(struct user_namespace *mnt_userns,
  309. struct inode *old_dir,
  310. struct dentry *old_dentry,
  311. struct inode *new_dir,
  312. struct dentry *new_dentry,
  313. unsigned int flags)
  314. {
  315. struct orangefs_kernel_op_s *new_op;
  316. struct iattr iattr;
  317. int ret;
  318. if (flags)
  319. return -EINVAL;
  320. gossip_debug(GOSSIP_NAME_DEBUG,
  321. "orangefs_rename: called (%pd2 => %pd2) ct=%d\n",
  322. old_dentry, new_dentry, d_count(new_dentry));
  323. memset(&iattr, 0, sizeof iattr);
  324. iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME;
  325. iattr.ia_mtime = iattr.ia_ctime = current_time(new_dir);
  326. __orangefs_setattr(new_dir, &iattr);
  327. new_op = op_alloc(ORANGEFS_VFS_OP_RENAME);
  328. if (!new_op)
  329. return -EINVAL;
  330. new_op->upcall.req.rename.old_parent_refn = ORANGEFS_I(old_dir)->refn;
  331. new_op->upcall.req.rename.new_parent_refn = ORANGEFS_I(new_dir)->refn;
  332. strncpy(new_op->upcall.req.rename.d_old_name,
  333. old_dentry->d_name.name,
  334. ORANGEFS_NAME_MAX - 1);
  335. strncpy(new_op->upcall.req.rename.d_new_name,
  336. new_dentry->d_name.name,
  337. ORANGEFS_NAME_MAX - 1);
  338. ret = service_operation(new_op,
  339. "orangefs_rename",
  340. get_interruptible_flag(old_dentry->d_inode));
  341. gossip_debug(GOSSIP_NAME_DEBUG,
  342. "orangefs_rename: got downcall status %d\n",
  343. ret);
  344. if (new_dentry->d_inode)
  345. new_dentry->d_inode->i_ctime = current_time(new_dentry->d_inode);
  346. op_release(new_op);
  347. return ret;
  348. }
  349. /* ORANGEFS implementation of VFS inode operations for directories */
  350. const struct inode_operations orangefs_dir_inode_operations = {
  351. .lookup = orangefs_lookup,
  352. .get_acl = orangefs_get_acl,
  353. .set_acl = orangefs_set_acl,
  354. .create = orangefs_create,
  355. .unlink = orangefs_unlink,
  356. .symlink = orangefs_symlink,
  357. .mkdir = orangefs_mkdir,
  358. .rmdir = orangefs_unlink,
  359. .rename = orangefs_rename,
  360. .setattr = orangefs_setattr,
  361. .getattr = orangefs_getattr,
  362. .listxattr = orangefs_listxattr,
  363. .permission = orangefs_permission,
  364. .update_time = orangefs_update_time,
  365. };