namei.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. ORANGEFS_I(inode)->getattr_time = jiffies - 1;
  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. gossip_debug(GOSSIP_NAME_DEBUG,
  269. "Inode (Symlink) %pU -> %pd\n",
  270. get_khandle_from_ino(inode),
  271. dentry);
  272. SetMtimeFlag(parent);
  273. dir->i_mtime = dir->i_ctime = current_time(dir);
  274. mark_inode_dirty_sync(dir);
  275. ret = 0;
  276. out:
  277. op_release(new_op);
  278. return ret;
  279. }
  280. static int orangefs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  281. {
  282. struct orangefs_inode_s *parent = ORANGEFS_I(dir);
  283. struct orangefs_kernel_op_s *new_op;
  284. struct inode *inode;
  285. int ret;
  286. new_op = op_alloc(ORANGEFS_VFS_OP_MKDIR);
  287. if (!new_op)
  288. return -ENOMEM;
  289. new_op->upcall.req.mkdir.parent_refn = parent->refn;
  290. fill_default_sys_attrs(new_op->upcall.req.mkdir.attributes,
  291. ORANGEFS_TYPE_DIRECTORY, mode);
  292. strncpy(new_op->upcall.req.mkdir.d_name,
  293. dentry->d_name.name, ORANGEFS_NAME_MAX);
  294. ret = service_operation(new_op, __func__, get_interruptible_flag(dir));
  295. gossip_debug(GOSSIP_NAME_DEBUG,
  296. "Mkdir Got ORANGEFS handle %pU on fsid %d\n",
  297. &new_op->downcall.resp.mkdir.refn.khandle,
  298. new_op->downcall.resp.mkdir.refn.fs_id);
  299. if (ret < 0) {
  300. gossip_debug(GOSSIP_NAME_DEBUG,
  301. "%s: failed with error code %d\n",
  302. __func__, ret);
  303. goto out;
  304. }
  305. inode = orangefs_new_inode(dir->i_sb, dir, S_IFDIR | mode, 0,
  306. &new_op->downcall.resp.mkdir.refn);
  307. if (IS_ERR(inode)) {
  308. gossip_err("*** Failed to allocate orangefs dir inode\n");
  309. ret = PTR_ERR(inode);
  310. goto out;
  311. }
  312. gossip_debug(GOSSIP_NAME_DEBUG,
  313. "Assigned dir inode new number of %pU\n",
  314. get_khandle_from_ino(inode));
  315. d_instantiate(dentry, inode);
  316. unlock_new_inode(inode);
  317. orangefs_set_timeout(dentry);
  318. ORANGEFS_I(inode)->getattr_time = jiffies - 1;
  319. gossip_debug(GOSSIP_NAME_DEBUG,
  320. "Inode (Directory) %pU -> %pd\n",
  321. get_khandle_from_ino(inode),
  322. dentry);
  323. /*
  324. * NOTE: we have no good way to keep nlink consistent for directories
  325. * across clients; keep constant at 1.
  326. */
  327. SetMtimeFlag(parent);
  328. dir->i_mtime = dir->i_ctime = current_time(dir);
  329. mark_inode_dirty_sync(dir);
  330. out:
  331. op_release(new_op);
  332. return ret;
  333. }
  334. static int orangefs_rename(struct inode *old_dir,
  335. struct dentry *old_dentry,
  336. struct inode *new_dir,
  337. struct dentry *new_dentry,
  338. unsigned int flags)
  339. {
  340. struct orangefs_kernel_op_s *new_op;
  341. int ret;
  342. if (flags)
  343. return -EINVAL;
  344. gossip_debug(GOSSIP_NAME_DEBUG,
  345. "orangefs_rename: called (%pd2 => %pd2) ct=%d\n",
  346. old_dentry, new_dentry, d_count(new_dentry));
  347. ORANGEFS_I(new_dentry->d_parent->d_inode)->getattr_time = jiffies - 1;
  348. new_op = op_alloc(ORANGEFS_VFS_OP_RENAME);
  349. if (!new_op)
  350. return -EINVAL;
  351. new_op->upcall.req.rename.old_parent_refn = ORANGEFS_I(old_dir)->refn;
  352. new_op->upcall.req.rename.new_parent_refn = ORANGEFS_I(new_dir)->refn;
  353. strncpy(new_op->upcall.req.rename.d_old_name,
  354. old_dentry->d_name.name,
  355. ORANGEFS_NAME_MAX);
  356. strncpy(new_op->upcall.req.rename.d_new_name,
  357. new_dentry->d_name.name,
  358. ORANGEFS_NAME_MAX);
  359. ret = service_operation(new_op,
  360. "orangefs_rename",
  361. get_interruptible_flag(old_dentry->d_inode));
  362. gossip_debug(GOSSIP_NAME_DEBUG,
  363. "orangefs_rename: got downcall status %d\n",
  364. ret);
  365. if (new_dentry->d_inode)
  366. new_dentry->d_inode->i_ctime = current_time(new_dentry->d_inode);
  367. op_release(new_op);
  368. return ret;
  369. }
  370. /* ORANGEFS implementation of VFS inode operations for directories */
  371. const struct inode_operations orangefs_dir_inode_operations = {
  372. .lookup = orangefs_lookup,
  373. .get_acl = orangefs_get_acl,
  374. .set_acl = orangefs_set_acl,
  375. .create = orangefs_create,
  376. .unlink = orangefs_unlink,
  377. .symlink = orangefs_symlink,
  378. .mkdir = orangefs_mkdir,
  379. .rmdir = orangefs_unlink,
  380. .rename = orangefs_rename,
  381. .setattr = orangefs_setattr,
  382. .getattr = orangefs_getattr,
  383. .listxattr = orangefs_listxattr,
  384. .permission = orangefs_permission,
  385. };