file.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/sched.h>
  4. #include <linux/slab.h>
  5. #include <linux/file.h>
  6. #include <linux/mount.h>
  7. #include <linux/namei.h>
  8. #include <linux/writeback.h>
  9. #include <linux/falloc.h>
  10. #include "super.h"
  11. #include "mds_client.h"
  12. #include "cache.h"
  13. /*
  14. * Ceph file operations
  15. *
  16. * Implement basic open/close functionality, and implement
  17. * read/write.
  18. *
  19. * We implement three modes of file I/O:
  20. * - buffered uses the generic_file_aio_{read,write} helpers
  21. *
  22. * - synchronous is used when there is multi-client read/write
  23. * sharing, avoids the page cache, and synchronously waits for an
  24. * ack from the OSD.
  25. *
  26. * - direct io takes the variant of the sync path that references
  27. * user pages directly.
  28. *
  29. * fsync() flushes and waits on dirty pages, but just queues metadata
  30. * for writeback: since the MDS can recover size and mtime there is no
  31. * need to wait for MDS acknowledgement.
  32. */
  33. /*
  34. * Calculate the length sum of direct io vectors that can
  35. * be combined into one page vector.
  36. */
  37. static size_t dio_get_pagev_size(const struct iov_iter *it)
  38. {
  39. const struct iovec *iov = it->iov;
  40. const struct iovec *iovend = iov + it->nr_segs;
  41. size_t size;
  42. size = iov->iov_len - it->iov_offset;
  43. /*
  44. * An iov can be page vectored when both the current tail
  45. * and the next base are page aligned.
  46. */
  47. while (PAGE_ALIGNED((iov->iov_base + iov->iov_len)) &&
  48. (++iov < iovend && PAGE_ALIGNED((iov->iov_base)))) {
  49. size += iov->iov_len;
  50. }
  51. dout("dio_get_pagevlen len = %zu\n", size);
  52. return size;
  53. }
  54. /*
  55. * Allocate a page vector based on (@it, @nbytes).
  56. * The return value is the tuple describing a page vector,
  57. * that is (@pages, @page_align, @num_pages).
  58. */
  59. static struct page **
  60. dio_get_pages_alloc(const struct iov_iter *it, size_t nbytes,
  61. size_t *page_align, int *num_pages)
  62. {
  63. struct iov_iter tmp_it = *it;
  64. size_t align;
  65. struct page **pages;
  66. int ret = 0, idx, npages;
  67. align = (unsigned long)(it->iov->iov_base + it->iov_offset) &
  68. (PAGE_SIZE - 1);
  69. npages = calc_pages_for(align, nbytes);
  70. pages = kmalloc(sizeof(*pages) * npages, GFP_KERNEL);
  71. if (!pages) {
  72. pages = vmalloc(sizeof(*pages) * npages);
  73. if (!pages)
  74. return ERR_PTR(-ENOMEM);
  75. }
  76. for (idx = 0; idx < npages; ) {
  77. size_t start;
  78. ret = iov_iter_get_pages(&tmp_it, pages + idx, nbytes,
  79. npages - idx, &start);
  80. if (ret < 0)
  81. goto fail;
  82. iov_iter_advance(&tmp_it, ret);
  83. nbytes -= ret;
  84. idx += (ret + start + PAGE_SIZE - 1) / PAGE_SIZE;
  85. }
  86. BUG_ON(nbytes != 0);
  87. *num_pages = npages;
  88. *page_align = align;
  89. dout("dio_get_pages_alloc: got %d pages align %zu\n", npages, align);
  90. return pages;
  91. fail:
  92. ceph_put_page_vector(pages, idx, false);
  93. return ERR_PTR(ret);
  94. }
  95. /*
  96. * Prepare an open request. Preallocate ceph_cap to avoid an
  97. * inopportune ENOMEM later.
  98. */
  99. static struct ceph_mds_request *
  100. prepare_open_request(struct super_block *sb, int flags, int create_mode)
  101. {
  102. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  103. struct ceph_mds_client *mdsc = fsc->mdsc;
  104. struct ceph_mds_request *req;
  105. int want_auth = USE_ANY_MDS;
  106. int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
  107. if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
  108. want_auth = USE_AUTH_MDS;
  109. req = ceph_mdsc_create_request(mdsc, op, want_auth);
  110. if (IS_ERR(req))
  111. goto out;
  112. req->r_fmode = ceph_flags_to_mode(flags);
  113. req->r_args.open.flags = cpu_to_le32(flags);
  114. req->r_args.open.mode = cpu_to_le32(create_mode);
  115. out:
  116. return req;
  117. }
  118. /*
  119. * initialize private struct file data.
  120. * if we fail, clean up by dropping fmode reference on the ceph_inode
  121. */
  122. static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
  123. {
  124. struct ceph_file_info *cf;
  125. int ret = 0;
  126. switch (inode->i_mode & S_IFMT) {
  127. case S_IFREG:
  128. ceph_fscache_register_inode_cookie(inode);
  129. ceph_fscache_file_set_cookie(inode, file);
  130. case S_IFDIR:
  131. dout("init_file %p %p 0%o (regular)\n", inode, file,
  132. inode->i_mode);
  133. cf = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
  134. if (cf == NULL) {
  135. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  136. return -ENOMEM;
  137. }
  138. cf->fmode = fmode;
  139. cf->next_offset = 2;
  140. cf->readdir_cache_idx = -1;
  141. file->private_data = cf;
  142. BUG_ON(inode->i_fop->release != ceph_release);
  143. break;
  144. case S_IFLNK:
  145. dout("init_file %p %p 0%o (symlink)\n", inode, file,
  146. inode->i_mode);
  147. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  148. break;
  149. default:
  150. dout("init_file %p %p 0%o (special)\n", inode, file,
  151. inode->i_mode);
  152. /*
  153. * we need to drop the open ref now, since we don't
  154. * have .release set to ceph_release.
  155. */
  156. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  157. BUG_ON(inode->i_fop->release == ceph_release);
  158. /* call the proper open fop */
  159. ret = inode->i_fop->open(inode, file);
  160. }
  161. return ret;
  162. }
  163. /*
  164. * try renew caps after session gets killed.
  165. */
  166. int ceph_renew_caps(struct inode *inode)
  167. {
  168. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  169. struct ceph_inode_info *ci = ceph_inode(inode);
  170. struct ceph_mds_request *req;
  171. int err, flags, wanted;
  172. spin_lock(&ci->i_ceph_lock);
  173. wanted = __ceph_caps_file_wanted(ci);
  174. if (__ceph_is_any_real_caps(ci) &&
  175. (!(wanted & CEPH_CAP_ANY_WR) == 0 || ci->i_auth_cap)) {
  176. int issued = __ceph_caps_issued(ci, NULL);
  177. spin_unlock(&ci->i_ceph_lock);
  178. dout("renew caps %p want %s issued %s updating mds_wanted\n",
  179. inode, ceph_cap_string(wanted), ceph_cap_string(issued));
  180. ceph_check_caps(ci, 0, NULL);
  181. return 0;
  182. }
  183. spin_unlock(&ci->i_ceph_lock);
  184. flags = 0;
  185. if ((wanted & CEPH_CAP_FILE_RD) && (wanted & CEPH_CAP_FILE_WR))
  186. flags = O_RDWR;
  187. else if (wanted & CEPH_CAP_FILE_RD)
  188. flags = O_RDONLY;
  189. else if (wanted & CEPH_CAP_FILE_WR)
  190. flags = O_WRONLY;
  191. #ifdef O_LAZY
  192. if (wanted & CEPH_CAP_FILE_LAZYIO)
  193. flags |= O_LAZY;
  194. #endif
  195. req = prepare_open_request(inode->i_sb, flags, 0);
  196. if (IS_ERR(req)) {
  197. err = PTR_ERR(req);
  198. goto out;
  199. }
  200. req->r_inode = inode;
  201. ihold(inode);
  202. req->r_num_caps = 1;
  203. req->r_fmode = -1;
  204. err = ceph_mdsc_do_request(mdsc, NULL, req);
  205. ceph_mdsc_put_request(req);
  206. out:
  207. dout("renew caps %p open result=%d\n", inode, err);
  208. return err < 0 ? err : 0;
  209. }
  210. /*
  211. * If we already have the requisite capabilities, we can satisfy
  212. * the open request locally (no need to request new caps from the
  213. * MDS). We do, however, need to inform the MDS (asynchronously)
  214. * if our wanted caps set expands.
  215. */
  216. int ceph_open(struct inode *inode, struct file *file)
  217. {
  218. struct ceph_inode_info *ci = ceph_inode(inode);
  219. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  220. struct ceph_mds_client *mdsc = fsc->mdsc;
  221. struct ceph_mds_request *req;
  222. struct ceph_file_info *cf = file->private_data;
  223. int err;
  224. int flags, fmode, wanted;
  225. if (cf) {
  226. dout("open file %p is already opened\n", file);
  227. return 0;
  228. }
  229. /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
  230. flags = file->f_flags & ~(O_CREAT|O_EXCL);
  231. if (S_ISDIR(inode->i_mode))
  232. flags = O_DIRECTORY; /* mds likes to know */
  233. dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
  234. ceph_vinop(inode), file, flags, file->f_flags);
  235. fmode = ceph_flags_to_mode(flags);
  236. wanted = ceph_caps_for_mode(fmode);
  237. /* snapped files are read-only */
  238. if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
  239. return -EROFS;
  240. /* trivially open snapdir */
  241. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  242. spin_lock(&ci->i_ceph_lock);
  243. __ceph_get_fmode(ci, fmode);
  244. spin_unlock(&ci->i_ceph_lock);
  245. return ceph_init_file(inode, file, fmode);
  246. }
  247. /*
  248. * No need to block if we have caps on the auth MDS (for
  249. * write) or any MDS (for read). Update wanted set
  250. * asynchronously.
  251. */
  252. spin_lock(&ci->i_ceph_lock);
  253. if (__ceph_is_any_real_caps(ci) &&
  254. (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
  255. int mds_wanted = __ceph_caps_mds_wanted(ci, true);
  256. int issued = __ceph_caps_issued(ci, NULL);
  257. dout("open %p fmode %d want %s issued %s using existing\n",
  258. inode, fmode, ceph_cap_string(wanted),
  259. ceph_cap_string(issued));
  260. __ceph_get_fmode(ci, fmode);
  261. spin_unlock(&ci->i_ceph_lock);
  262. /* adjust wanted? */
  263. if ((issued & wanted) != wanted &&
  264. (mds_wanted & wanted) != wanted &&
  265. ceph_snap(inode) != CEPH_SNAPDIR)
  266. ceph_check_caps(ci, 0, NULL);
  267. return ceph_init_file(inode, file, fmode);
  268. } else if (ceph_snap(inode) != CEPH_NOSNAP &&
  269. (ci->i_snap_caps & wanted) == wanted) {
  270. __ceph_get_fmode(ci, fmode);
  271. spin_unlock(&ci->i_ceph_lock);
  272. return ceph_init_file(inode, file, fmode);
  273. }
  274. spin_unlock(&ci->i_ceph_lock);
  275. dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
  276. req = prepare_open_request(inode->i_sb, flags, 0);
  277. if (IS_ERR(req)) {
  278. err = PTR_ERR(req);
  279. goto out;
  280. }
  281. req->r_inode = inode;
  282. ihold(inode);
  283. req->r_num_caps = 1;
  284. err = ceph_mdsc_do_request(mdsc, NULL, req);
  285. if (!err)
  286. err = ceph_init_file(inode, file, req->r_fmode);
  287. ceph_mdsc_put_request(req);
  288. dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
  289. out:
  290. return err;
  291. }
  292. /*
  293. * Do a lookup + open with a single request. If we get a non-existent
  294. * file or symlink, return 1 so the VFS can retry.
  295. */
  296. int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
  297. struct file *file, unsigned flags, umode_t mode,
  298. int *opened)
  299. {
  300. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  301. struct ceph_mds_client *mdsc = fsc->mdsc;
  302. struct ceph_mds_request *req;
  303. struct dentry *dn;
  304. struct ceph_acls_info acls = {};
  305. int mask;
  306. int err;
  307. dout("atomic_open %p dentry %p '%pd' %s flags %d mode 0%o\n",
  308. dir, dentry, dentry,
  309. d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
  310. if (dentry->d_name.len > NAME_MAX)
  311. return -ENAMETOOLONG;
  312. if (flags & O_CREAT) {
  313. err = ceph_pre_init_acls(dir, &mode, &acls);
  314. if (err < 0)
  315. return err;
  316. }
  317. /* do the open */
  318. req = prepare_open_request(dir->i_sb, flags, mode);
  319. if (IS_ERR(req)) {
  320. err = PTR_ERR(req);
  321. goto out_acl;
  322. }
  323. req->r_dentry = dget(dentry);
  324. req->r_num_caps = 2;
  325. if (flags & O_CREAT) {
  326. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  327. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  328. if (acls.pagelist) {
  329. req->r_pagelist = acls.pagelist;
  330. acls.pagelist = NULL;
  331. }
  332. }
  333. mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
  334. if (ceph_security_xattr_wanted(dir))
  335. mask |= CEPH_CAP_XATTR_SHARED;
  336. req->r_args.open.mask = cpu_to_le32(mask);
  337. req->r_parent = dir;
  338. set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
  339. err = ceph_mdsc_do_request(mdsc,
  340. (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
  341. req);
  342. err = ceph_handle_snapdir(req, dentry, err);
  343. if (err)
  344. goto out_req;
  345. if ((flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  346. err = ceph_handle_notrace_create(dir, dentry);
  347. if (d_in_lookup(dentry)) {
  348. dn = ceph_finish_lookup(req, dentry, err);
  349. if (IS_ERR(dn))
  350. err = PTR_ERR(dn);
  351. } else {
  352. /* we were given a hashed negative dentry */
  353. dn = NULL;
  354. }
  355. if (err)
  356. goto out_req;
  357. if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
  358. /* make vfs retry on splice, ENOENT, or symlink */
  359. dout("atomic_open finish_no_open on dn %p\n", dn);
  360. err = finish_no_open(file, dn);
  361. } else {
  362. dout("atomic_open finish_open on dn %p\n", dn);
  363. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  364. ceph_init_inode_acls(d_inode(dentry), &acls);
  365. *opened |= FILE_CREATED;
  366. }
  367. err = finish_open(file, dentry, ceph_open, opened);
  368. }
  369. out_req:
  370. if (!req->r_err && req->r_target_inode)
  371. ceph_put_fmode(ceph_inode(req->r_target_inode), req->r_fmode);
  372. ceph_mdsc_put_request(req);
  373. out_acl:
  374. ceph_release_acls_info(&acls);
  375. dout("atomic_open result=%d\n", err);
  376. return err;
  377. }
  378. int ceph_release(struct inode *inode, struct file *file)
  379. {
  380. struct ceph_inode_info *ci = ceph_inode(inode);
  381. struct ceph_file_info *cf = file->private_data;
  382. dout("release inode %p file %p\n", inode, file);
  383. ceph_put_fmode(ci, cf->fmode);
  384. if (cf->last_readdir)
  385. ceph_mdsc_put_request(cf->last_readdir);
  386. kfree(cf->last_name);
  387. kfree(cf->dir_info);
  388. kmem_cache_free(ceph_file_cachep, cf);
  389. /* wake up anyone waiting for caps on this inode */
  390. wake_up_all(&ci->i_cap_wq);
  391. return 0;
  392. }
  393. enum {
  394. HAVE_RETRIED = 1,
  395. CHECK_EOF = 2,
  396. READ_INLINE = 3,
  397. };
  398. /*
  399. * Read a range of bytes striped over one or more objects. Iterate over
  400. * objects we stripe over. (That's not atomic, but good enough for now.)
  401. *
  402. * If we get a short result from the OSD, check against i_size; we need to
  403. * only return a short read to the caller if we hit EOF.
  404. */
  405. static int striped_read(struct inode *inode,
  406. u64 pos, u64 len,
  407. struct page **pages, int num_pages,
  408. int page_align, int *checkeof)
  409. {
  410. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  411. struct ceph_inode_info *ci = ceph_inode(inode);
  412. u64 this_len;
  413. loff_t i_size;
  414. int page_idx;
  415. int ret, read = 0;
  416. bool hit_stripe, was_short;
  417. /*
  418. * we may need to do multiple reads. not atomic, unfortunately.
  419. */
  420. more:
  421. this_len = len;
  422. page_idx = (page_align + read) >> PAGE_SHIFT;
  423. ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
  424. &ci->i_layout, pos, &this_len,
  425. ci->i_truncate_seq, ci->i_truncate_size,
  426. pages + page_idx, num_pages - page_idx,
  427. ((page_align + read) & ~PAGE_MASK));
  428. if (ret == -ENOENT)
  429. ret = 0;
  430. hit_stripe = this_len < len;
  431. was_short = ret >= 0 && ret < this_len;
  432. dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, len, read,
  433. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  434. i_size = i_size_read(inode);
  435. if (ret >= 0) {
  436. if (was_short && (pos + ret < i_size)) {
  437. int zlen = min(this_len - ret, i_size - pos - ret);
  438. int zoff = page_align + read + ret;
  439. dout(" zero gap %llu to %llu\n",
  440. pos + ret, pos + ret + zlen);
  441. ceph_zero_page_vector_range(zoff, zlen, pages);
  442. ret += zlen;
  443. }
  444. read += ret;
  445. pos += ret;
  446. len -= ret;
  447. /* hit stripe and need continue*/
  448. if (len && hit_stripe && pos < i_size)
  449. goto more;
  450. }
  451. if (read > 0) {
  452. ret = read;
  453. /* did we bounce off eof? */
  454. if (pos + len > i_size)
  455. *checkeof = CHECK_EOF;
  456. }
  457. dout("striped_read returns %d\n", ret);
  458. return ret;
  459. }
  460. /*
  461. * Completely synchronous read and write methods. Direct from __user
  462. * buffer to osd, or directly to user pages (if O_DIRECT).
  463. *
  464. * If the read spans object boundary, just do multiple reads.
  465. */
  466. static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,
  467. int *checkeof)
  468. {
  469. struct file *file = iocb->ki_filp;
  470. struct inode *inode = file_inode(file);
  471. struct page **pages;
  472. u64 off = iocb->ki_pos;
  473. int num_pages;
  474. ssize_t ret;
  475. size_t len = iov_iter_count(to);
  476. dout("sync_read on file %p %llu~%u %s\n", file, off,
  477. (unsigned)len,
  478. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  479. if (!len)
  480. return 0;
  481. /*
  482. * flush any page cache pages in this range. this
  483. * will make concurrent normal and sync io slow,
  484. * but it will at least behave sensibly when they are
  485. * in sequence.
  486. */
  487. ret = filemap_write_and_wait_range(inode->i_mapping, off,
  488. off + len);
  489. if (ret < 0)
  490. return ret;
  491. if (unlikely(to->type & ITER_PIPE)) {
  492. size_t page_off;
  493. ret = iov_iter_get_pages_alloc(to, &pages, len,
  494. &page_off);
  495. if (ret <= 0)
  496. return -ENOMEM;
  497. num_pages = DIV_ROUND_UP(ret + page_off, PAGE_SIZE);
  498. ret = striped_read(inode, off, ret, pages, num_pages,
  499. page_off, checkeof);
  500. if (ret > 0) {
  501. iov_iter_advance(to, ret);
  502. off += ret;
  503. } else {
  504. iov_iter_advance(to, 0);
  505. }
  506. ceph_put_page_vector(pages, num_pages, false);
  507. } else {
  508. num_pages = calc_pages_for(off, len);
  509. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  510. if (IS_ERR(pages))
  511. return PTR_ERR(pages);
  512. ret = striped_read(inode, off, len, pages, num_pages,
  513. (off & ~PAGE_MASK), checkeof);
  514. if (ret > 0) {
  515. int l, k = 0;
  516. size_t left = ret;
  517. while (left) {
  518. size_t page_off = off & ~PAGE_MASK;
  519. size_t copy = min_t(size_t, left,
  520. PAGE_SIZE - page_off);
  521. l = copy_page_to_iter(pages[k++], page_off,
  522. copy, to);
  523. off += l;
  524. left -= l;
  525. if (l < copy)
  526. break;
  527. }
  528. }
  529. ceph_release_page_vector(pages, num_pages);
  530. }
  531. if (off > iocb->ki_pos) {
  532. ret = off - iocb->ki_pos;
  533. iocb->ki_pos = off;
  534. }
  535. dout("sync_read result %zd\n", ret);
  536. return ret;
  537. }
  538. struct ceph_aio_request {
  539. struct kiocb *iocb;
  540. size_t total_len;
  541. int write;
  542. int error;
  543. struct list_head osd_reqs;
  544. unsigned num_reqs;
  545. atomic_t pending_reqs;
  546. struct timespec mtime;
  547. struct ceph_cap_flush *prealloc_cf;
  548. };
  549. struct ceph_aio_work {
  550. struct work_struct work;
  551. struct ceph_osd_request *req;
  552. };
  553. static void ceph_aio_retry_work(struct work_struct *work);
  554. static void ceph_aio_complete(struct inode *inode,
  555. struct ceph_aio_request *aio_req)
  556. {
  557. struct ceph_inode_info *ci = ceph_inode(inode);
  558. int ret;
  559. if (!atomic_dec_and_test(&aio_req->pending_reqs))
  560. return;
  561. ret = aio_req->error;
  562. if (!ret)
  563. ret = aio_req->total_len;
  564. dout("ceph_aio_complete %p rc %d\n", inode, ret);
  565. if (ret >= 0 && aio_req->write) {
  566. int dirty;
  567. loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
  568. if (endoff > i_size_read(inode)) {
  569. if (ceph_inode_set_size(inode, endoff))
  570. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  571. }
  572. spin_lock(&ci->i_ceph_lock);
  573. ci->i_inline_version = CEPH_INLINE_NONE;
  574. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  575. &aio_req->prealloc_cf);
  576. spin_unlock(&ci->i_ceph_lock);
  577. if (dirty)
  578. __mark_inode_dirty(inode, dirty);
  579. }
  580. ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
  581. CEPH_CAP_FILE_RD));
  582. aio_req->iocb->ki_complete(aio_req->iocb, ret, 0);
  583. ceph_free_cap_flush(aio_req->prealloc_cf);
  584. kfree(aio_req);
  585. }
  586. static void ceph_aio_complete_req(struct ceph_osd_request *req)
  587. {
  588. int rc = req->r_result;
  589. struct inode *inode = req->r_inode;
  590. struct ceph_aio_request *aio_req = req->r_priv;
  591. struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
  592. int num_pages = calc_pages_for((u64)osd_data->alignment,
  593. osd_data->length);
  594. dout("ceph_aio_complete_req %p rc %d bytes %llu\n",
  595. inode, rc, osd_data->length);
  596. if (rc == -EOLDSNAPC) {
  597. struct ceph_aio_work *aio_work;
  598. BUG_ON(!aio_req->write);
  599. aio_work = kmalloc(sizeof(*aio_work), GFP_NOFS);
  600. if (aio_work) {
  601. INIT_WORK(&aio_work->work, ceph_aio_retry_work);
  602. aio_work->req = req;
  603. queue_work(ceph_inode_to_client(inode)->wb_wq,
  604. &aio_work->work);
  605. return;
  606. }
  607. rc = -ENOMEM;
  608. } else if (!aio_req->write) {
  609. if (rc == -ENOENT)
  610. rc = 0;
  611. if (rc >= 0 && osd_data->length > rc) {
  612. int zoff = osd_data->alignment + rc;
  613. int zlen = osd_data->length - rc;
  614. /*
  615. * If read is satisfied by single OSD request,
  616. * it can pass EOF. Otherwise read is within
  617. * i_size.
  618. */
  619. if (aio_req->num_reqs == 1) {
  620. loff_t i_size = i_size_read(inode);
  621. loff_t endoff = aio_req->iocb->ki_pos + rc;
  622. if (endoff < i_size)
  623. zlen = min_t(size_t, zlen,
  624. i_size - endoff);
  625. aio_req->total_len = rc + zlen;
  626. }
  627. if (zlen > 0)
  628. ceph_zero_page_vector_range(zoff, zlen,
  629. osd_data->pages);
  630. }
  631. }
  632. ceph_put_page_vector(osd_data->pages, num_pages, !aio_req->write);
  633. ceph_osdc_put_request(req);
  634. if (rc < 0)
  635. cmpxchg(&aio_req->error, 0, rc);
  636. ceph_aio_complete(inode, aio_req);
  637. return;
  638. }
  639. static void ceph_aio_retry_work(struct work_struct *work)
  640. {
  641. struct ceph_aio_work *aio_work =
  642. container_of(work, struct ceph_aio_work, work);
  643. struct ceph_osd_request *orig_req = aio_work->req;
  644. struct ceph_aio_request *aio_req = orig_req->r_priv;
  645. struct inode *inode = orig_req->r_inode;
  646. struct ceph_inode_info *ci = ceph_inode(inode);
  647. struct ceph_snap_context *snapc;
  648. struct ceph_osd_request *req;
  649. int ret;
  650. spin_lock(&ci->i_ceph_lock);
  651. if (__ceph_have_pending_cap_snap(ci)) {
  652. struct ceph_cap_snap *capsnap =
  653. list_last_entry(&ci->i_cap_snaps,
  654. struct ceph_cap_snap,
  655. ci_item);
  656. snapc = ceph_get_snap_context(capsnap->context);
  657. } else {
  658. BUG_ON(!ci->i_head_snapc);
  659. snapc = ceph_get_snap_context(ci->i_head_snapc);
  660. }
  661. spin_unlock(&ci->i_ceph_lock);
  662. req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 2,
  663. false, GFP_NOFS);
  664. if (!req) {
  665. ret = -ENOMEM;
  666. req = orig_req;
  667. goto out;
  668. }
  669. req->r_flags = CEPH_OSD_FLAG_ORDERSNAP | CEPH_OSD_FLAG_WRITE;
  670. ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
  671. ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
  672. ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
  673. if (ret) {
  674. ceph_osdc_put_request(req);
  675. req = orig_req;
  676. goto out;
  677. }
  678. req->r_ops[0] = orig_req->r_ops[0];
  679. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC, 0);
  680. req->r_mtime = aio_req->mtime;
  681. req->r_data_offset = req->r_ops[0].extent.offset;
  682. ceph_osdc_put_request(orig_req);
  683. req->r_callback = ceph_aio_complete_req;
  684. req->r_inode = inode;
  685. req->r_priv = aio_req;
  686. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  687. out:
  688. if (ret < 0) {
  689. req->r_result = ret;
  690. ceph_aio_complete_req(req);
  691. }
  692. ceph_put_snap_context(snapc);
  693. kfree(aio_work);
  694. }
  695. static ssize_t
  696. ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
  697. struct ceph_snap_context *snapc,
  698. struct ceph_cap_flush **pcf)
  699. {
  700. struct file *file = iocb->ki_filp;
  701. struct inode *inode = file_inode(file);
  702. struct ceph_inode_info *ci = ceph_inode(inode);
  703. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  704. struct ceph_vino vino;
  705. struct ceph_osd_request *req;
  706. struct page **pages;
  707. struct ceph_aio_request *aio_req = NULL;
  708. int num_pages = 0;
  709. int flags;
  710. int ret;
  711. struct timespec mtime = current_time(inode);
  712. size_t count = iov_iter_count(iter);
  713. loff_t pos = iocb->ki_pos;
  714. bool write = iov_iter_rw(iter) == WRITE;
  715. if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  716. return -EROFS;
  717. dout("sync_direct_read_write (%s) on file %p %lld~%u\n",
  718. (write ? "write" : "read"), file, pos, (unsigned)count);
  719. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  720. if (ret < 0)
  721. return ret;
  722. if (write) {
  723. int ret2 = invalidate_inode_pages2_range(inode->i_mapping,
  724. pos >> PAGE_SHIFT,
  725. (pos + count) >> PAGE_SHIFT);
  726. if (ret2 < 0)
  727. dout("invalidate_inode_pages2_range returned %d\n", ret2);
  728. flags = CEPH_OSD_FLAG_ORDERSNAP | CEPH_OSD_FLAG_WRITE;
  729. } else {
  730. flags = CEPH_OSD_FLAG_READ;
  731. }
  732. while (iov_iter_count(iter) > 0) {
  733. u64 size = dio_get_pagev_size(iter);
  734. size_t start = 0;
  735. ssize_t len;
  736. vino = ceph_vino(inode);
  737. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  738. vino, pos, &size, 0,
  739. /*include a 'startsync' command*/
  740. write ? 2 : 1,
  741. write ? CEPH_OSD_OP_WRITE :
  742. CEPH_OSD_OP_READ,
  743. flags, snapc,
  744. ci->i_truncate_seq,
  745. ci->i_truncate_size,
  746. false);
  747. if (IS_ERR(req)) {
  748. ret = PTR_ERR(req);
  749. break;
  750. }
  751. len = size;
  752. pages = dio_get_pages_alloc(iter, len, &start, &num_pages);
  753. if (IS_ERR(pages)) {
  754. ceph_osdc_put_request(req);
  755. ret = PTR_ERR(pages);
  756. break;
  757. }
  758. /*
  759. * To simplify error handling, allow AIO when IO within i_size
  760. * or IO can be satisfied by single OSD request.
  761. */
  762. if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
  763. (len == count || pos + count <= i_size_read(inode))) {
  764. aio_req = kzalloc(sizeof(*aio_req), GFP_KERNEL);
  765. if (aio_req) {
  766. aio_req->iocb = iocb;
  767. aio_req->write = write;
  768. INIT_LIST_HEAD(&aio_req->osd_reqs);
  769. if (write) {
  770. aio_req->mtime = mtime;
  771. swap(aio_req->prealloc_cf, *pcf);
  772. }
  773. }
  774. /* ignore error */
  775. }
  776. if (write) {
  777. /*
  778. * throw out any page cache pages in this range. this
  779. * may block.
  780. */
  781. truncate_inode_pages_range(inode->i_mapping, pos,
  782. (pos+len) | (PAGE_SIZE - 1));
  783. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC, 0);
  784. req->r_mtime = mtime;
  785. }
  786. osd_req_op_extent_osd_data_pages(req, 0, pages, len, start,
  787. false, false);
  788. if (aio_req) {
  789. aio_req->total_len += len;
  790. aio_req->num_reqs++;
  791. atomic_inc(&aio_req->pending_reqs);
  792. req->r_callback = ceph_aio_complete_req;
  793. req->r_inode = inode;
  794. req->r_priv = aio_req;
  795. list_add_tail(&req->r_unsafe_item, &aio_req->osd_reqs);
  796. pos += len;
  797. iov_iter_advance(iter, len);
  798. continue;
  799. }
  800. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  801. if (!ret)
  802. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  803. size = i_size_read(inode);
  804. if (!write) {
  805. if (ret == -ENOENT)
  806. ret = 0;
  807. if (ret >= 0 && ret < len && pos + ret < size) {
  808. int zlen = min_t(size_t, len - ret,
  809. size - pos - ret);
  810. ceph_zero_page_vector_range(start + ret, zlen,
  811. pages);
  812. ret += zlen;
  813. }
  814. if (ret >= 0)
  815. len = ret;
  816. }
  817. ceph_put_page_vector(pages, num_pages, !write);
  818. ceph_osdc_put_request(req);
  819. if (ret < 0)
  820. break;
  821. pos += len;
  822. iov_iter_advance(iter, len);
  823. if (!write && pos >= size)
  824. break;
  825. if (write && pos > size) {
  826. if (ceph_inode_set_size(inode, pos))
  827. ceph_check_caps(ceph_inode(inode),
  828. CHECK_CAPS_AUTHONLY,
  829. NULL);
  830. }
  831. }
  832. if (aio_req) {
  833. LIST_HEAD(osd_reqs);
  834. if (aio_req->num_reqs == 0) {
  835. kfree(aio_req);
  836. return ret;
  837. }
  838. ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
  839. CEPH_CAP_FILE_RD);
  840. list_splice(&aio_req->osd_reqs, &osd_reqs);
  841. while (!list_empty(&osd_reqs)) {
  842. req = list_first_entry(&osd_reqs,
  843. struct ceph_osd_request,
  844. r_unsafe_item);
  845. list_del_init(&req->r_unsafe_item);
  846. if (ret >= 0)
  847. ret = ceph_osdc_start_request(req->r_osdc,
  848. req, false);
  849. if (ret < 0) {
  850. req->r_result = ret;
  851. ceph_aio_complete_req(req);
  852. }
  853. }
  854. return -EIOCBQUEUED;
  855. }
  856. if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
  857. ret = pos - iocb->ki_pos;
  858. iocb->ki_pos = pos;
  859. }
  860. return ret;
  861. }
  862. /*
  863. * Synchronous write, straight from __user pointer or user pages.
  864. *
  865. * If write spans object boundary, just do multiple writes. (For a
  866. * correct atomic write, we should e.g. take write locks on all
  867. * objects, rollback on failure, etc.)
  868. */
  869. static ssize_t
  870. ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
  871. struct ceph_snap_context *snapc)
  872. {
  873. struct file *file = iocb->ki_filp;
  874. struct inode *inode = file_inode(file);
  875. struct ceph_inode_info *ci = ceph_inode(inode);
  876. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  877. struct ceph_vino vino;
  878. struct ceph_osd_request *req;
  879. struct page **pages;
  880. u64 len;
  881. int num_pages;
  882. int written = 0;
  883. int flags;
  884. int check_caps = 0;
  885. int ret;
  886. struct timespec mtime = current_time(inode);
  887. size_t count = iov_iter_count(from);
  888. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  889. return -EROFS;
  890. dout("sync_write on file %p %lld~%u\n", file, pos, (unsigned)count);
  891. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  892. if (ret < 0)
  893. return ret;
  894. ret = invalidate_inode_pages2_range(inode->i_mapping,
  895. pos >> PAGE_SHIFT,
  896. (pos + count) >> PAGE_SHIFT);
  897. if (ret < 0)
  898. dout("invalidate_inode_pages2_range returned %d\n", ret);
  899. flags = CEPH_OSD_FLAG_ORDERSNAP | CEPH_OSD_FLAG_WRITE;
  900. while ((len = iov_iter_count(from)) > 0) {
  901. size_t left;
  902. int n;
  903. vino = ceph_vino(inode);
  904. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  905. vino, pos, &len, 0, 1,
  906. CEPH_OSD_OP_WRITE, flags, snapc,
  907. ci->i_truncate_seq,
  908. ci->i_truncate_size,
  909. false);
  910. if (IS_ERR(req)) {
  911. ret = PTR_ERR(req);
  912. break;
  913. }
  914. /*
  915. * write from beginning of first page,
  916. * regardless of io alignment
  917. */
  918. num_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  919. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  920. if (IS_ERR(pages)) {
  921. ret = PTR_ERR(pages);
  922. goto out;
  923. }
  924. left = len;
  925. for (n = 0; n < num_pages; n++) {
  926. size_t plen = min_t(size_t, left, PAGE_SIZE);
  927. ret = copy_page_from_iter(pages[n], 0, plen, from);
  928. if (ret != plen) {
  929. ret = -EFAULT;
  930. break;
  931. }
  932. left -= ret;
  933. }
  934. if (ret < 0) {
  935. ceph_release_page_vector(pages, num_pages);
  936. goto out;
  937. }
  938. req->r_inode = inode;
  939. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
  940. false, true);
  941. req->r_mtime = mtime;
  942. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  943. if (!ret)
  944. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  945. out:
  946. ceph_osdc_put_request(req);
  947. if (ret == 0) {
  948. pos += len;
  949. written += len;
  950. if (pos > i_size_read(inode)) {
  951. check_caps = ceph_inode_set_size(inode, pos);
  952. if (check_caps)
  953. ceph_check_caps(ceph_inode(inode),
  954. CHECK_CAPS_AUTHONLY,
  955. NULL);
  956. }
  957. } else
  958. break;
  959. }
  960. if (ret != -EOLDSNAPC && written > 0) {
  961. ret = written;
  962. iocb->ki_pos = pos;
  963. }
  964. return ret;
  965. }
  966. /*
  967. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  968. * Atomically grab references, so that those bits are not released
  969. * back to the MDS mid-read.
  970. *
  971. * Hmm, the sync read case isn't actually async... should it be?
  972. */
  973. static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
  974. {
  975. struct file *filp = iocb->ki_filp;
  976. struct ceph_file_info *fi = filp->private_data;
  977. size_t len = iov_iter_count(to);
  978. struct inode *inode = file_inode(filp);
  979. struct ceph_inode_info *ci = ceph_inode(inode);
  980. struct page *pinned_page = NULL;
  981. ssize_t ret;
  982. int want, got = 0;
  983. int retry_op = 0, read = 0;
  984. again:
  985. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  986. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
  987. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  988. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  989. else
  990. want = CEPH_CAP_FILE_CACHE;
  991. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
  992. if (ret < 0)
  993. return ret;
  994. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  995. (iocb->ki_flags & IOCB_DIRECT) ||
  996. (fi->flags & CEPH_F_SYNC)) {
  997. dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  998. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  999. ceph_cap_string(got));
  1000. if (ci->i_inline_version == CEPH_INLINE_NONE) {
  1001. if (!retry_op && (iocb->ki_flags & IOCB_DIRECT)) {
  1002. ret = ceph_direct_read_write(iocb, to,
  1003. NULL, NULL);
  1004. if (ret >= 0 && ret < len)
  1005. retry_op = CHECK_EOF;
  1006. } else {
  1007. ret = ceph_sync_read(iocb, to, &retry_op);
  1008. }
  1009. } else {
  1010. retry_op = READ_INLINE;
  1011. }
  1012. } else {
  1013. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  1014. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1015. ceph_cap_string(got));
  1016. current->journal_info = filp;
  1017. ret = generic_file_read_iter(iocb, to);
  1018. current->journal_info = NULL;
  1019. }
  1020. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  1021. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  1022. if (pinned_page) {
  1023. put_page(pinned_page);
  1024. pinned_page = NULL;
  1025. }
  1026. ceph_put_cap_refs(ci, got);
  1027. if (retry_op > HAVE_RETRIED && ret >= 0) {
  1028. int statret;
  1029. struct page *page = NULL;
  1030. loff_t i_size;
  1031. if (retry_op == READ_INLINE) {
  1032. page = __page_cache_alloc(GFP_KERNEL);
  1033. if (!page)
  1034. return -ENOMEM;
  1035. }
  1036. statret = __ceph_do_getattr(inode, page,
  1037. CEPH_STAT_CAP_INLINE_DATA, !!page);
  1038. if (statret < 0) {
  1039. if (page)
  1040. __free_page(page);
  1041. if (statret == -ENODATA) {
  1042. BUG_ON(retry_op != READ_INLINE);
  1043. goto again;
  1044. }
  1045. return statret;
  1046. }
  1047. i_size = i_size_read(inode);
  1048. if (retry_op == READ_INLINE) {
  1049. BUG_ON(ret > 0 || read > 0);
  1050. if (iocb->ki_pos < i_size &&
  1051. iocb->ki_pos < PAGE_SIZE) {
  1052. loff_t end = min_t(loff_t, i_size,
  1053. iocb->ki_pos + len);
  1054. end = min_t(loff_t, end, PAGE_SIZE);
  1055. if (statret < end)
  1056. zero_user_segment(page, statret, end);
  1057. ret = copy_page_to_iter(page,
  1058. iocb->ki_pos & ~PAGE_MASK,
  1059. end - iocb->ki_pos, to);
  1060. iocb->ki_pos += ret;
  1061. read += ret;
  1062. }
  1063. if (iocb->ki_pos < i_size && read < len) {
  1064. size_t zlen = min_t(size_t, len - read,
  1065. i_size - iocb->ki_pos);
  1066. ret = iov_iter_zero(zlen, to);
  1067. iocb->ki_pos += ret;
  1068. read += ret;
  1069. }
  1070. __free_pages(page, 0);
  1071. return read;
  1072. }
  1073. /* hit EOF or hole? */
  1074. if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
  1075. ret < len) {
  1076. dout("sync_read hit hole, ppos %lld < size %lld"
  1077. ", reading more\n", iocb->ki_pos, i_size);
  1078. read += ret;
  1079. len -= ret;
  1080. retry_op = HAVE_RETRIED;
  1081. goto again;
  1082. }
  1083. }
  1084. if (ret >= 0)
  1085. ret += read;
  1086. return ret;
  1087. }
  1088. /*
  1089. * Take cap references to avoid releasing caps to MDS mid-write.
  1090. *
  1091. * If we are synchronous, and write with an old snap context, the OSD
  1092. * may return EOLDSNAPC. In that case, retry the write.. _after_
  1093. * dropping our cap refs and allowing the pending snap to logically
  1094. * complete _before_ this write occurs.
  1095. *
  1096. * If we are near ENOSPC, write synchronously.
  1097. */
  1098. static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1099. {
  1100. struct file *file = iocb->ki_filp;
  1101. struct ceph_file_info *fi = file->private_data;
  1102. struct inode *inode = file_inode(file);
  1103. struct ceph_inode_info *ci = ceph_inode(inode);
  1104. struct ceph_osd_client *osdc =
  1105. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  1106. struct ceph_cap_flush *prealloc_cf;
  1107. ssize_t count, written = 0;
  1108. int err, want, got;
  1109. loff_t pos;
  1110. if (ceph_snap(inode) != CEPH_NOSNAP)
  1111. return -EROFS;
  1112. prealloc_cf = ceph_alloc_cap_flush();
  1113. if (!prealloc_cf)
  1114. return -ENOMEM;
  1115. inode_lock(inode);
  1116. /* We can write back this queue in page reclaim */
  1117. current->backing_dev_info = inode_to_bdi(inode);
  1118. if (iocb->ki_flags & IOCB_APPEND) {
  1119. err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1120. if (err < 0)
  1121. goto out;
  1122. }
  1123. err = generic_write_checks(iocb, from);
  1124. if (err <= 0)
  1125. goto out;
  1126. pos = iocb->ki_pos;
  1127. count = iov_iter_count(from);
  1128. err = file_remove_privs(file);
  1129. if (err)
  1130. goto out;
  1131. err = file_update_time(file);
  1132. if (err)
  1133. goto out;
  1134. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1135. err = ceph_uninline_data(file, NULL);
  1136. if (err < 0)
  1137. goto out;
  1138. }
  1139. retry_snap:
  1140. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL)) {
  1141. err = -ENOSPC;
  1142. goto out;
  1143. }
  1144. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  1145. inode, ceph_vinop(inode), pos, count, i_size_read(inode));
  1146. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1147. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1148. else
  1149. want = CEPH_CAP_FILE_BUFFER;
  1150. got = 0;
  1151. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, pos + count,
  1152. &got, NULL);
  1153. if (err < 0)
  1154. goto out;
  1155. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  1156. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  1157. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  1158. (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC)) {
  1159. struct ceph_snap_context *snapc;
  1160. struct iov_iter data;
  1161. inode_unlock(inode);
  1162. spin_lock(&ci->i_ceph_lock);
  1163. if (__ceph_have_pending_cap_snap(ci)) {
  1164. struct ceph_cap_snap *capsnap =
  1165. list_last_entry(&ci->i_cap_snaps,
  1166. struct ceph_cap_snap,
  1167. ci_item);
  1168. snapc = ceph_get_snap_context(capsnap->context);
  1169. } else {
  1170. BUG_ON(!ci->i_head_snapc);
  1171. snapc = ceph_get_snap_context(ci->i_head_snapc);
  1172. }
  1173. spin_unlock(&ci->i_ceph_lock);
  1174. /* we might need to revert back to that point */
  1175. data = *from;
  1176. if (iocb->ki_flags & IOCB_DIRECT)
  1177. written = ceph_direct_read_write(iocb, &data, snapc,
  1178. &prealloc_cf);
  1179. else
  1180. written = ceph_sync_write(iocb, &data, pos, snapc);
  1181. if (written == -EOLDSNAPC) {
  1182. dout("aio_write %p %llx.%llx %llu~%u"
  1183. "got EOLDSNAPC, retrying\n",
  1184. inode, ceph_vinop(inode),
  1185. pos, (unsigned)count);
  1186. inode_lock(inode);
  1187. goto retry_snap;
  1188. }
  1189. if (written > 0)
  1190. iov_iter_advance(from, written);
  1191. ceph_put_snap_context(snapc);
  1192. } else {
  1193. /*
  1194. * No need to acquire the i_truncate_mutex. Because
  1195. * the MDS revokes Fwb caps before sending truncate
  1196. * message to us. We can't get Fwb cap while there
  1197. * are pending vmtruncate. So write and vmtruncate
  1198. * can not run at the same time
  1199. */
  1200. written = generic_perform_write(file, from, pos);
  1201. if (likely(written >= 0))
  1202. iocb->ki_pos = pos + written;
  1203. inode_unlock(inode);
  1204. }
  1205. if (written >= 0) {
  1206. int dirty;
  1207. spin_lock(&ci->i_ceph_lock);
  1208. ci->i_inline_version = CEPH_INLINE_NONE;
  1209. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1210. &prealloc_cf);
  1211. spin_unlock(&ci->i_ceph_lock);
  1212. if (dirty)
  1213. __mark_inode_dirty(inode, dirty);
  1214. }
  1215. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  1216. inode, ceph_vinop(inode), pos, (unsigned)count,
  1217. ceph_cap_string(got));
  1218. ceph_put_cap_refs(ci, got);
  1219. if (written >= 0) {
  1220. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_NEARFULL))
  1221. iocb->ki_flags |= IOCB_DSYNC;
  1222. written = generic_write_sync(iocb, written);
  1223. }
  1224. goto out_unlocked;
  1225. out:
  1226. inode_unlock(inode);
  1227. out_unlocked:
  1228. ceph_free_cap_flush(prealloc_cf);
  1229. current->backing_dev_info = NULL;
  1230. return written ? written : err;
  1231. }
  1232. /*
  1233. * llseek. be sure to verify file size on SEEK_END.
  1234. */
  1235. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  1236. {
  1237. struct inode *inode = file->f_mapping->host;
  1238. loff_t i_size;
  1239. loff_t ret;
  1240. inode_lock(inode);
  1241. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  1242. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1243. if (ret < 0)
  1244. goto out;
  1245. }
  1246. i_size = i_size_read(inode);
  1247. switch (whence) {
  1248. case SEEK_END:
  1249. offset += i_size;
  1250. break;
  1251. case SEEK_CUR:
  1252. /*
  1253. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  1254. * position-querying operation. Avoid rewriting the "same"
  1255. * f_pos value back to the file because a concurrent read(),
  1256. * write() or lseek() might have altered it
  1257. */
  1258. if (offset == 0) {
  1259. ret = file->f_pos;
  1260. goto out;
  1261. }
  1262. offset += file->f_pos;
  1263. break;
  1264. case SEEK_DATA:
  1265. if (offset >= i_size) {
  1266. ret = -ENXIO;
  1267. goto out;
  1268. }
  1269. break;
  1270. case SEEK_HOLE:
  1271. if (offset >= i_size) {
  1272. ret = -ENXIO;
  1273. goto out;
  1274. }
  1275. offset = i_size;
  1276. break;
  1277. }
  1278. ret = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  1279. out:
  1280. inode_unlock(inode);
  1281. return ret;
  1282. }
  1283. static inline void ceph_zero_partial_page(
  1284. struct inode *inode, loff_t offset, unsigned size)
  1285. {
  1286. struct page *page;
  1287. pgoff_t index = offset >> PAGE_SHIFT;
  1288. page = find_lock_page(inode->i_mapping, index);
  1289. if (page) {
  1290. wait_on_page_writeback(page);
  1291. zero_user(page, offset & (PAGE_SIZE - 1), size);
  1292. unlock_page(page);
  1293. put_page(page);
  1294. }
  1295. }
  1296. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  1297. loff_t length)
  1298. {
  1299. loff_t nearly = round_up(offset, PAGE_SIZE);
  1300. if (offset < nearly) {
  1301. loff_t size = nearly - offset;
  1302. if (length < size)
  1303. size = length;
  1304. ceph_zero_partial_page(inode, offset, size);
  1305. offset += size;
  1306. length -= size;
  1307. }
  1308. if (length >= PAGE_SIZE) {
  1309. loff_t size = round_down(length, PAGE_SIZE);
  1310. truncate_pagecache_range(inode, offset, offset + size - 1);
  1311. offset += size;
  1312. length -= size;
  1313. }
  1314. if (length)
  1315. ceph_zero_partial_page(inode, offset, length);
  1316. }
  1317. static int ceph_zero_partial_object(struct inode *inode,
  1318. loff_t offset, loff_t *length)
  1319. {
  1320. struct ceph_inode_info *ci = ceph_inode(inode);
  1321. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1322. struct ceph_osd_request *req;
  1323. int ret = 0;
  1324. loff_t zero = 0;
  1325. int op;
  1326. if (!length) {
  1327. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  1328. length = &zero;
  1329. } else {
  1330. op = CEPH_OSD_OP_ZERO;
  1331. }
  1332. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1333. ceph_vino(inode),
  1334. offset, length,
  1335. 0, 1, op,
  1336. CEPH_OSD_FLAG_WRITE,
  1337. NULL, 0, 0, false);
  1338. if (IS_ERR(req)) {
  1339. ret = PTR_ERR(req);
  1340. goto out;
  1341. }
  1342. req->r_mtime = inode->i_mtime;
  1343. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1344. if (!ret) {
  1345. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1346. if (ret == -ENOENT)
  1347. ret = 0;
  1348. }
  1349. ceph_osdc_put_request(req);
  1350. out:
  1351. return ret;
  1352. }
  1353. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  1354. {
  1355. int ret = 0;
  1356. struct ceph_inode_info *ci = ceph_inode(inode);
  1357. s32 stripe_unit = ci->i_layout.stripe_unit;
  1358. s32 stripe_count = ci->i_layout.stripe_count;
  1359. s32 object_size = ci->i_layout.object_size;
  1360. u64 object_set_size = object_size * stripe_count;
  1361. u64 nearly, t;
  1362. /* round offset up to next period boundary */
  1363. nearly = offset + object_set_size - 1;
  1364. t = nearly;
  1365. nearly -= do_div(t, object_set_size);
  1366. while (length && offset < nearly) {
  1367. loff_t size = length;
  1368. ret = ceph_zero_partial_object(inode, offset, &size);
  1369. if (ret < 0)
  1370. return ret;
  1371. offset += size;
  1372. length -= size;
  1373. }
  1374. while (length >= object_set_size) {
  1375. int i;
  1376. loff_t pos = offset;
  1377. for (i = 0; i < stripe_count; ++i) {
  1378. ret = ceph_zero_partial_object(inode, pos, NULL);
  1379. if (ret < 0)
  1380. return ret;
  1381. pos += stripe_unit;
  1382. }
  1383. offset += object_set_size;
  1384. length -= object_set_size;
  1385. }
  1386. while (length) {
  1387. loff_t size = length;
  1388. ret = ceph_zero_partial_object(inode, offset, &size);
  1389. if (ret < 0)
  1390. return ret;
  1391. offset += size;
  1392. length -= size;
  1393. }
  1394. return ret;
  1395. }
  1396. static long ceph_fallocate(struct file *file, int mode,
  1397. loff_t offset, loff_t length)
  1398. {
  1399. struct ceph_file_info *fi = file->private_data;
  1400. struct inode *inode = file_inode(file);
  1401. struct ceph_inode_info *ci = ceph_inode(inode);
  1402. struct ceph_osd_client *osdc =
  1403. &ceph_inode_to_client(inode)->client->osdc;
  1404. struct ceph_cap_flush *prealloc_cf;
  1405. int want, got = 0;
  1406. int dirty;
  1407. int ret = 0;
  1408. loff_t endoff = 0;
  1409. loff_t size;
  1410. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1411. return -EOPNOTSUPP;
  1412. if (!S_ISREG(inode->i_mode))
  1413. return -EOPNOTSUPP;
  1414. prealloc_cf = ceph_alloc_cap_flush();
  1415. if (!prealloc_cf)
  1416. return -ENOMEM;
  1417. inode_lock(inode);
  1418. if (ceph_snap(inode) != CEPH_NOSNAP) {
  1419. ret = -EROFS;
  1420. goto unlock;
  1421. }
  1422. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) &&
  1423. !(mode & FALLOC_FL_PUNCH_HOLE)) {
  1424. ret = -ENOSPC;
  1425. goto unlock;
  1426. }
  1427. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1428. ret = ceph_uninline_data(file, NULL);
  1429. if (ret < 0)
  1430. goto unlock;
  1431. }
  1432. size = i_size_read(inode);
  1433. if (!(mode & FALLOC_FL_KEEP_SIZE))
  1434. endoff = offset + length;
  1435. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1436. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1437. else
  1438. want = CEPH_CAP_FILE_BUFFER;
  1439. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, endoff, &got, NULL);
  1440. if (ret < 0)
  1441. goto unlock;
  1442. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1443. if (offset < size)
  1444. ceph_zero_pagecache_range(inode, offset, length);
  1445. ret = ceph_zero_objects(inode, offset, length);
  1446. } else if (endoff > size) {
  1447. truncate_pagecache_range(inode, size, -1);
  1448. if (ceph_inode_set_size(inode, endoff))
  1449. ceph_check_caps(ceph_inode(inode),
  1450. CHECK_CAPS_AUTHONLY, NULL);
  1451. }
  1452. if (!ret) {
  1453. spin_lock(&ci->i_ceph_lock);
  1454. ci->i_inline_version = CEPH_INLINE_NONE;
  1455. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1456. &prealloc_cf);
  1457. spin_unlock(&ci->i_ceph_lock);
  1458. if (dirty)
  1459. __mark_inode_dirty(inode, dirty);
  1460. }
  1461. ceph_put_cap_refs(ci, got);
  1462. unlock:
  1463. inode_unlock(inode);
  1464. ceph_free_cap_flush(prealloc_cf);
  1465. return ret;
  1466. }
  1467. const struct file_operations ceph_file_fops = {
  1468. .open = ceph_open,
  1469. .release = ceph_release,
  1470. .llseek = ceph_llseek,
  1471. .read_iter = ceph_read_iter,
  1472. .write_iter = ceph_write_iter,
  1473. .mmap = ceph_mmap,
  1474. .fsync = ceph_fsync,
  1475. .lock = ceph_lock,
  1476. .flock = ceph_flock,
  1477. .splice_read = generic_file_splice_read,
  1478. .splice_write = iter_file_splice_write,
  1479. .unlocked_ioctl = ceph_ioctl,
  1480. .compat_ioctl = ceph_ioctl,
  1481. .fallocate = ceph_fallocate,
  1482. };