file.c 43 KB

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