inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * (C) 2001 Clemson University and The University of Chicago
  3. *
  4. * See COPYING in top-level directory.
  5. */
  6. /*
  7. * Linux VFS inode operations.
  8. */
  9. #include "protocol.h"
  10. #include "orangefs-kernel.h"
  11. #include "orangefs-bufmap.h"
  12. static int read_one_page(struct page *page)
  13. {
  14. int ret;
  15. int max_block;
  16. ssize_t bytes_read = 0;
  17. struct inode *inode = page->mapping->host;
  18. const __u32 blocksize = PAGE_SIZE; /* inode->i_blksize */
  19. const __u32 blockbits = PAGE_SHIFT; /* inode->i_blkbits */
  20. struct iov_iter to;
  21. struct bio_vec bv = {.bv_page = page, .bv_len = PAGE_SIZE};
  22. iov_iter_bvec(&to, ITER_BVEC | READ, &bv, 1, PAGE_SIZE);
  23. gossip_debug(GOSSIP_INODE_DEBUG,
  24. "orangefs_readpage called with page %p\n",
  25. page);
  26. max_block = ((inode->i_size / blocksize) + 1);
  27. if (page->index < max_block) {
  28. loff_t blockptr_offset = (((loff_t) page->index) << blockbits);
  29. bytes_read = orangefs_inode_read(inode,
  30. &to,
  31. &blockptr_offset,
  32. inode->i_size);
  33. }
  34. /* this will only zero remaining unread portions of the page data */
  35. iov_iter_zero(~0U, &to);
  36. /* takes care of potential aliasing */
  37. flush_dcache_page(page);
  38. if (bytes_read < 0) {
  39. ret = bytes_read;
  40. SetPageError(page);
  41. } else {
  42. SetPageUptodate(page);
  43. if (PageError(page))
  44. ClearPageError(page);
  45. ret = 0;
  46. }
  47. /* unlock the page after the ->readpage() routine completes */
  48. unlock_page(page);
  49. return ret;
  50. }
  51. static int orangefs_readpage(struct file *file, struct page *page)
  52. {
  53. return read_one_page(page);
  54. }
  55. static int orangefs_readpages(struct file *file,
  56. struct address_space *mapping,
  57. struct list_head *pages,
  58. unsigned nr_pages)
  59. {
  60. int page_idx;
  61. int ret;
  62. gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_readpages called\n");
  63. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  64. struct page *page;
  65. page = list_entry(pages->prev, struct page, lru);
  66. list_del(&page->lru);
  67. if (!add_to_page_cache(page,
  68. mapping,
  69. page->index,
  70. readahead_gfp_mask(mapping))) {
  71. ret = read_one_page(page);
  72. gossip_debug(GOSSIP_INODE_DEBUG,
  73. "failure adding page to cache, read_one_page returned: %d\n",
  74. ret);
  75. } else {
  76. put_page(page);
  77. }
  78. }
  79. BUG_ON(!list_empty(pages));
  80. return 0;
  81. }
  82. static void orangefs_invalidatepage(struct page *page,
  83. unsigned int offset,
  84. unsigned int length)
  85. {
  86. gossip_debug(GOSSIP_INODE_DEBUG,
  87. "orangefs_invalidatepage called on page %p "
  88. "(offset is %u)\n",
  89. page,
  90. offset);
  91. ClearPageUptodate(page);
  92. ClearPageMappedToDisk(page);
  93. return;
  94. }
  95. static int orangefs_releasepage(struct page *page, gfp_t foo)
  96. {
  97. gossip_debug(GOSSIP_INODE_DEBUG,
  98. "orangefs_releasepage called on page %p\n",
  99. page);
  100. return 0;
  101. }
  102. /*
  103. * Having a direct_IO entry point in the address_space_operations
  104. * struct causes the kernel to allows us to use O_DIRECT on
  105. * open. Nothing will ever call this thing, but in the future we
  106. * will need to be able to use O_DIRECT on open in order to support
  107. * AIO. Modeled after NFS, they do this too.
  108. */
  109. static ssize_t orangefs_direct_IO(struct kiocb *iocb,
  110. struct iov_iter *iter)
  111. {
  112. gossip_debug(GOSSIP_INODE_DEBUG,
  113. "orangefs_direct_IO: %s\n",
  114. iocb->ki_filp->f_path.dentry->d_name.name);
  115. return -EINVAL;
  116. }
  117. struct backing_dev_info orangefs_backing_dev_info = {
  118. .name = "orangefs",
  119. .ra_pages = 0,
  120. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  121. };
  122. /** ORANGEFS2 implementation of address space operations */
  123. const struct address_space_operations orangefs_address_operations = {
  124. .readpage = orangefs_readpage,
  125. .readpages = orangefs_readpages,
  126. .invalidatepage = orangefs_invalidatepage,
  127. .releasepage = orangefs_releasepage,
  128. .direct_IO = orangefs_direct_IO,
  129. };
  130. static int orangefs_setattr_size(struct inode *inode, struct iattr *iattr)
  131. {
  132. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  133. struct orangefs_kernel_op_s *new_op;
  134. loff_t orig_size;
  135. int ret = -EINVAL;
  136. gossip_debug(GOSSIP_INODE_DEBUG,
  137. "%s: %pU: Handle is %pU | fs_id %d | size is %llu\n",
  138. __func__,
  139. get_khandle_from_ino(inode),
  140. &orangefs_inode->refn.khandle,
  141. orangefs_inode->refn.fs_id,
  142. iattr->ia_size);
  143. /* Ensure that we have a up to date size, so we know if it changed. */
  144. ret = orangefs_inode_getattr(inode, 0, 1);
  145. if (ret == -ESTALE)
  146. ret = -EIO;
  147. if (ret) {
  148. gossip_err("%s: orangefs_inode_getattr failed, ret:%d:.\n",
  149. __func__, ret);
  150. return ret;
  151. }
  152. orig_size = i_size_read(inode);
  153. truncate_setsize(inode, iattr->ia_size);
  154. new_op = op_alloc(ORANGEFS_VFS_OP_TRUNCATE);
  155. if (!new_op)
  156. return -ENOMEM;
  157. new_op->upcall.req.truncate.refn = orangefs_inode->refn;
  158. new_op->upcall.req.truncate.size = (__s64) iattr->ia_size;
  159. ret = service_operation(new_op, __func__,
  160. get_interruptible_flag(inode));
  161. /*
  162. * the truncate has no downcall members to retrieve, but
  163. * the status value tells us if it went through ok or not
  164. */
  165. gossip_debug(GOSSIP_INODE_DEBUG,
  166. "orangefs: orangefs_truncate got return value of %d\n",
  167. ret);
  168. op_release(new_op);
  169. if (ret != 0)
  170. return ret;
  171. if (orig_size != i_size_read(inode))
  172. iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  173. return ret;
  174. }
  175. /*
  176. * Change attributes of an object referenced by dentry.
  177. */
  178. int orangefs_setattr(struct dentry *dentry, struct iattr *iattr)
  179. {
  180. int ret = -EINVAL;
  181. struct inode *inode = dentry->d_inode;
  182. gossip_debug(GOSSIP_INODE_DEBUG,
  183. "orangefs_setattr: called on %s\n",
  184. dentry->d_name.name);
  185. ret = inode_change_ok(inode, iattr);
  186. if (ret)
  187. goto out;
  188. if ((iattr->ia_valid & ATTR_SIZE) &&
  189. iattr->ia_size != i_size_read(inode)) {
  190. ret = orangefs_setattr_size(inode, iattr);
  191. if (ret)
  192. goto out;
  193. }
  194. setattr_copy(inode, iattr);
  195. mark_inode_dirty(inode);
  196. ret = orangefs_inode_setattr(inode, iattr);
  197. gossip_debug(GOSSIP_INODE_DEBUG,
  198. "orangefs_setattr: inode_setattr returned %d\n",
  199. ret);
  200. if (!ret && (iattr->ia_valid & ATTR_MODE))
  201. /* change mod on a file that has ACLs */
  202. ret = posix_acl_chmod(inode, inode->i_mode);
  203. out:
  204. gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_setattr: returning %d\n", ret);
  205. return ret;
  206. }
  207. /*
  208. * Obtain attributes of an object given a dentry
  209. */
  210. int orangefs_getattr(struct vfsmount *mnt,
  211. struct dentry *dentry,
  212. struct kstat *kstat)
  213. {
  214. int ret = -ENOENT;
  215. struct inode *inode = dentry->d_inode;
  216. struct orangefs_inode_s *orangefs_inode = NULL;
  217. gossip_debug(GOSSIP_INODE_DEBUG,
  218. "orangefs_getattr: called on %s\n",
  219. dentry->d_name.name);
  220. ret = orangefs_inode_getattr(inode, 0, 0);
  221. if (ret == 0) {
  222. generic_fillattr(inode, kstat);
  223. /* override block size reported to stat */
  224. orangefs_inode = ORANGEFS_I(inode);
  225. kstat->blksize = orangefs_inode->blksize;
  226. }
  227. return ret;
  228. }
  229. int orangefs_permission(struct inode *inode, int mask)
  230. {
  231. int ret;
  232. if (mask & MAY_NOT_BLOCK)
  233. return -ECHILD;
  234. gossip_debug(GOSSIP_INODE_DEBUG, "%s: refreshing\n", __func__);
  235. /* Make sure the permission (and other common attrs) are up to date. */
  236. ret = orangefs_inode_getattr(inode, 0, 0);
  237. if (ret < 0)
  238. return ret;
  239. return generic_permission(inode, mask);
  240. }
  241. /* ORANGEDS2 implementation of VFS inode operations for files */
  242. const struct inode_operations orangefs_file_inode_operations = {
  243. .get_acl = orangefs_get_acl,
  244. .set_acl = orangefs_set_acl,
  245. .setattr = orangefs_setattr,
  246. .getattr = orangefs_getattr,
  247. .setxattr = generic_setxattr,
  248. .getxattr = generic_getxattr,
  249. .listxattr = orangefs_listxattr,
  250. .removexattr = generic_removexattr,
  251. .permission = orangefs_permission,
  252. };
  253. static int orangefs_init_iops(struct inode *inode)
  254. {
  255. inode->i_mapping->a_ops = &orangefs_address_operations;
  256. switch (inode->i_mode & S_IFMT) {
  257. case S_IFREG:
  258. inode->i_op = &orangefs_file_inode_operations;
  259. inode->i_fop = &orangefs_file_operations;
  260. inode->i_blkbits = PAGE_SHIFT;
  261. break;
  262. case S_IFLNK:
  263. inode->i_op = &orangefs_symlink_inode_operations;
  264. break;
  265. case S_IFDIR:
  266. inode->i_op = &orangefs_dir_inode_operations;
  267. inode->i_fop = &orangefs_dir_operations;
  268. break;
  269. default:
  270. gossip_debug(GOSSIP_INODE_DEBUG,
  271. "%s: unsupported mode\n",
  272. __func__);
  273. return -EINVAL;
  274. }
  275. return 0;
  276. }
  277. /*
  278. * Given a ORANGEFS object identifier (fsid, handle), convert it into a ino_t type
  279. * that will be used as a hash-index from where the handle will
  280. * be searched for in the VFS hash table of inodes.
  281. */
  282. static inline ino_t orangefs_handle_hash(struct orangefs_object_kref *ref)
  283. {
  284. if (!ref)
  285. return 0;
  286. return orangefs_khandle_to_ino(&(ref->khandle));
  287. }
  288. /*
  289. * Called to set up an inode from iget5_locked.
  290. */
  291. static int orangefs_set_inode(struct inode *inode, void *data)
  292. {
  293. struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
  294. ORANGEFS_I(inode)->refn.fs_id = ref->fs_id;
  295. ORANGEFS_I(inode)->refn.khandle = ref->khandle;
  296. return 0;
  297. }
  298. /*
  299. * Called to determine if handles match.
  300. */
  301. static int orangefs_test_inode(struct inode *inode, void *data)
  302. {
  303. struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
  304. struct orangefs_inode_s *orangefs_inode = NULL;
  305. orangefs_inode = ORANGEFS_I(inode);
  306. return (!ORANGEFS_khandle_cmp(&(orangefs_inode->refn.khandle), &(ref->khandle))
  307. && orangefs_inode->refn.fs_id == ref->fs_id);
  308. }
  309. /*
  310. * Front-end to lookup the inode-cache maintained by the VFS using the ORANGEFS
  311. * file handle.
  312. *
  313. * @sb: the file system super block instance.
  314. * @ref: The ORANGEFS object for which we are trying to locate an inode structure.
  315. */
  316. struct inode *orangefs_iget(struct super_block *sb, struct orangefs_object_kref *ref)
  317. {
  318. struct inode *inode = NULL;
  319. unsigned long hash;
  320. int error;
  321. hash = orangefs_handle_hash(ref);
  322. inode = iget5_locked(sb, hash, orangefs_test_inode, orangefs_set_inode, ref);
  323. if (!inode || !(inode->i_state & I_NEW))
  324. return inode;
  325. error = orangefs_inode_getattr(inode, 1, 1);
  326. if (error) {
  327. iget_failed(inode);
  328. return ERR_PTR(error);
  329. }
  330. inode->i_ino = hash; /* needed for stat etc */
  331. orangefs_init_iops(inode);
  332. unlock_new_inode(inode);
  333. gossip_debug(GOSSIP_INODE_DEBUG,
  334. "iget handle %pU, fsid %d hash %ld i_ino %lu\n",
  335. &ref->khandle,
  336. ref->fs_id,
  337. hash,
  338. inode->i_ino);
  339. return inode;
  340. }
  341. /*
  342. * Allocate an inode for a newly created file and insert it into the inode hash.
  343. */
  344. struct inode *orangefs_new_inode(struct super_block *sb, struct inode *dir,
  345. int mode, dev_t dev, struct orangefs_object_kref *ref)
  346. {
  347. unsigned long hash = orangefs_handle_hash(ref);
  348. struct inode *inode;
  349. int error;
  350. gossip_debug(GOSSIP_INODE_DEBUG,
  351. "%s:(sb is %p | MAJOR(dev)=%u | MINOR(dev)=%u mode=%o)\n",
  352. __func__,
  353. sb,
  354. MAJOR(dev),
  355. MINOR(dev),
  356. mode);
  357. inode = new_inode(sb);
  358. if (!inode)
  359. return NULL;
  360. orangefs_set_inode(inode, ref);
  361. inode->i_ino = hash; /* needed for stat etc */
  362. error = orangefs_inode_getattr(inode, 1, 1);
  363. if (error)
  364. goto out_iput;
  365. orangefs_init_iops(inode);
  366. inode->i_mode = mode;
  367. inode->i_uid = current_fsuid();
  368. inode->i_gid = current_fsgid();
  369. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  370. inode->i_size = PAGE_SIZE;
  371. inode->i_rdev = dev;
  372. error = insert_inode_locked4(inode, hash, orangefs_test_inode, ref);
  373. if (error < 0)
  374. goto out_iput;
  375. gossip_debug(GOSSIP_INODE_DEBUG,
  376. "Initializing ACL's for inode %pU\n",
  377. get_khandle_from_ino(inode));
  378. orangefs_init_acl(inode, dir);
  379. return inode;
  380. out_iput:
  381. iput(inode);
  382. return ERR_PTR(error);
  383. }