namei.c 12 KB

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