inode.c 12 KB

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