inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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: %pD\n",
  114. iocb->ki_filp);
  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 %pd\n",
  184. dentry);
  185. ret = setattr_prepare(dentry, iattr);
  186. if (ret)
  187. goto out;
  188. if (iattr->ia_valid & ATTR_SIZE) {
  189. ret = orangefs_setattr_size(inode, iattr);
  190. if (ret)
  191. goto out;
  192. }
  193. setattr_copy(inode, iattr);
  194. mark_inode_dirty(inode);
  195. ret = orangefs_inode_setattr(inode, iattr);
  196. gossip_debug(GOSSIP_INODE_DEBUG,
  197. "orangefs_setattr: inode_setattr returned %d\n",
  198. ret);
  199. if (!ret && (iattr->ia_valid & ATTR_MODE))
  200. /* change mod on a file that has ACLs */
  201. ret = posix_acl_chmod(inode, inode->i_mode);
  202. out:
  203. gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_setattr: returning %d\n", ret);
  204. return ret;
  205. }
  206. /*
  207. * Obtain attributes of an object given a dentry
  208. */
  209. int orangefs_getattr(struct vfsmount *mnt,
  210. struct dentry *dentry,
  211. struct kstat *kstat)
  212. {
  213. int ret = -ENOENT;
  214. struct inode *inode = dentry->d_inode;
  215. struct orangefs_inode_s *orangefs_inode = NULL;
  216. gossip_debug(GOSSIP_INODE_DEBUG,
  217. "orangefs_getattr: called on %pd\n",
  218. dentry);
  219. ret = orangefs_inode_getattr(inode, 0, 0);
  220. if (ret == 0) {
  221. generic_fillattr(inode, kstat);
  222. /* override block size reported to stat */
  223. orangefs_inode = ORANGEFS_I(inode);
  224. kstat->blksize = orangefs_inode->blksize;
  225. }
  226. return ret;
  227. }
  228. int orangefs_permission(struct inode *inode, int mask)
  229. {
  230. int ret;
  231. if (mask & MAY_NOT_BLOCK)
  232. return -ECHILD;
  233. gossip_debug(GOSSIP_INODE_DEBUG, "%s: refreshing\n", __func__);
  234. /* Make sure the permission (and other common attrs) are up to date. */
  235. ret = orangefs_inode_getattr(inode, 0, 0);
  236. if (ret < 0)
  237. return ret;
  238. return generic_permission(inode, mask);
  239. }
  240. /* ORANGEDS2 implementation of VFS inode operations for files */
  241. const struct inode_operations orangefs_file_inode_operations = {
  242. .get_acl = orangefs_get_acl,
  243. .set_acl = orangefs_set_acl,
  244. .setattr = orangefs_setattr,
  245. .getattr = orangefs_getattr,
  246. .listxattr = orangefs_listxattr,
  247. .permission = orangefs_permission,
  248. };
  249. static int orangefs_init_iops(struct inode *inode)
  250. {
  251. inode->i_mapping->a_ops = &orangefs_address_operations;
  252. switch (inode->i_mode & S_IFMT) {
  253. case S_IFREG:
  254. inode->i_op = &orangefs_file_inode_operations;
  255. inode->i_fop = &orangefs_file_operations;
  256. inode->i_blkbits = PAGE_SHIFT;
  257. break;
  258. case S_IFLNK:
  259. inode->i_op = &orangefs_symlink_inode_operations;
  260. break;
  261. case S_IFDIR:
  262. inode->i_op = &orangefs_dir_inode_operations;
  263. inode->i_fop = &orangefs_dir_operations;
  264. break;
  265. default:
  266. gossip_debug(GOSSIP_INODE_DEBUG,
  267. "%s: unsupported mode\n",
  268. __func__);
  269. return -EINVAL;
  270. }
  271. return 0;
  272. }
  273. /*
  274. * Given a ORANGEFS object identifier (fsid, handle), convert it into a ino_t type
  275. * that will be used as a hash-index from where the handle will
  276. * be searched for in the VFS hash table of inodes.
  277. */
  278. static inline ino_t orangefs_handle_hash(struct orangefs_object_kref *ref)
  279. {
  280. if (!ref)
  281. return 0;
  282. return orangefs_khandle_to_ino(&(ref->khandle));
  283. }
  284. /*
  285. * Called to set up an inode from iget5_locked.
  286. */
  287. static int orangefs_set_inode(struct inode *inode, void *data)
  288. {
  289. struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
  290. ORANGEFS_I(inode)->refn.fs_id = ref->fs_id;
  291. ORANGEFS_I(inode)->refn.khandle = ref->khandle;
  292. return 0;
  293. }
  294. /*
  295. * Called to determine if handles match.
  296. */
  297. static int orangefs_test_inode(struct inode *inode, void *data)
  298. {
  299. struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
  300. struct orangefs_inode_s *orangefs_inode = NULL;
  301. orangefs_inode = ORANGEFS_I(inode);
  302. return (!ORANGEFS_khandle_cmp(&(orangefs_inode->refn.khandle), &(ref->khandle))
  303. && orangefs_inode->refn.fs_id == ref->fs_id);
  304. }
  305. /*
  306. * Front-end to lookup the inode-cache maintained by the VFS using the ORANGEFS
  307. * file handle.
  308. *
  309. * @sb: the file system super block instance.
  310. * @ref: The ORANGEFS object for which we are trying to locate an inode structure.
  311. */
  312. struct inode *orangefs_iget(struct super_block *sb, struct orangefs_object_kref *ref)
  313. {
  314. struct inode *inode = NULL;
  315. unsigned long hash;
  316. int error;
  317. hash = orangefs_handle_hash(ref);
  318. inode = iget5_locked(sb, hash, orangefs_test_inode, orangefs_set_inode, ref);
  319. if (!inode || !(inode->i_state & I_NEW))
  320. return inode;
  321. error = orangefs_inode_getattr(inode, 1, 1);
  322. if (error) {
  323. iget_failed(inode);
  324. return ERR_PTR(error);
  325. }
  326. inode->i_ino = hash; /* needed for stat etc */
  327. orangefs_init_iops(inode);
  328. unlock_new_inode(inode);
  329. gossip_debug(GOSSIP_INODE_DEBUG,
  330. "iget handle %pU, fsid %d hash %ld i_ino %lu\n",
  331. &ref->khandle,
  332. ref->fs_id,
  333. hash,
  334. inode->i_ino);
  335. return inode;
  336. }
  337. /*
  338. * Allocate an inode for a newly created file and insert it into the inode hash.
  339. */
  340. struct inode *orangefs_new_inode(struct super_block *sb, struct inode *dir,
  341. int mode, dev_t dev, struct orangefs_object_kref *ref)
  342. {
  343. unsigned long hash = orangefs_handle_hash(ref);
  344. struct inode *inode;
  345. int error;
  346. gossip_debug(GOSSIP_INODE_DEBUG,
  347. "%s:(sb is %p | MAJOR(dev)=%u | MINOR(dev)=%u mode=%o)\n",
  348. __func__,
  349. sb,
  350. MAJOR(dev),
  351. MINOR(dev),
  352. mode);
  353. inode = new_inode(sb);
  354. if (!inode)
  355. return NULL;
  356. orangefs_set_inode(inode, ref);
  357. inode->i_ino = hash; /* needed for stat etc */
  358. error = orangefs_inode_getattr(inode, 1, 1);
  359. if (error)
  360. goto out_iput;
  361. orangefs_init_iops(inode);
  362. inode->i_mode = mode;
  363. inode->i_uid = current_fsuid();
  364. inode->i_gid = current_fsgid();
  365. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  366. inode->i_size = PAGE_SIZE;
  367. inode->i_rdev = dev;
  368. error = insert_inode_locked4(inode, hash, orangefs_test_inode, ref);
  369. if (error < 0)
  370. goto out_iput;
  371. gossip_debug(GOSSIP_INODE_DEBUG,
  372. "Initializing ACL's for inode %pU\n",
  373. get_khandle_from_ino(inode));
  374. orangefs_init_acl(inode, dir);
  375. return inode;
  376. out_iput:
  377. iput(inode);
  378. return ERR_PTR(error);
  379. }