namei.c 12 KB

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