file.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  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);
  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. err = ceph_init_dentry(dentry);
  313. if (err < 0)
  314. return err;
  315. if (flags & O_CREAT) {
  316. err = ceph_pre_init_acls(dir, &mode, &acls);
  317. if (err < 0)
  318. return err;
  319. }
  320. /* do the open */
  321. req = prepare_open_request(dir->i_sb, flags, mode);
  322. if (IS_ERR(req)) {
  323. err = PTR_ERR(req);
  324. goto out_acl;
  325. }
  326. req->r_dentry = dget(dentry);
  327. req->r_num_caps = 2;
  328. if (flags & O_CREAT) {
  329. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  330. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  331. if (acls.pagelist) {
  332. req->r_pagelist = acls.pagelist;
  333. acls.pagelist = NULL;
  334. }
  335. }
  336. mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
  337. if (ceph_security_xattr_wanted(dir))
  338. mask |= CEPH_CAP_XATTR_SHARED;
  339. req->r_args.open.mask = cpu_to_le32(mask);
  340. req->r_locked_dir = dir; /* caller holds dir->i_mutex */
  341. err = ceph_mdsc_do_request(mdsc,
  342. (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
  343. req);
  344. err = ceph_handle_snapdir(req, dentry, err);
  345. if (err)
  346. goto out_req;
  347. if ((flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  348. err = ceph_handle_notrace_create(dir, dentry);
  349. if (d_in_lookup(dentry)) {
  350. dn = ceph_finish_lookup(req, dentry, err);
  351. if (IS_ERR(dn))
  352. err = PTR_ERR(dn);
  353. } else {
  354. /* we were given a hashed negative dentry */
  355. dn = NULL;
  356. }
  357. if (err)
  358. goto out_req;
  359. if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
  360. /* make vfs retry on splice, ENOENT, or symlink */
  361. dout("atomic_open finish_no_open on dn %p\n", dn);
  362. err = finish_no_open(file, dn);
  363. } else {
  364. dout("atomic_open finish_open on dn %p\n", dn);
  365. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  366. ceph_init_inode_acls(d_inode(dentry), &acls);
  367. *opened |= FILE_CREATED;
  368. }
  369. err = finish_open(file, dentry, ceph_open, opened);
  370. }
  371. out_req:
  372. if (!req->r_err && req->r_target_inode)
  373. ceph_put_fmode(ceph_inode(req->r_target_inode), req->r_fmode);
  374. ceph_mdsc_put_request(req);
  375. out_acl:
  376. ceph_release_acls_info(&acls);
  377. dout("atomic_open result=%d\n", err);
  378. return err;
  379. }
  380. int ceph_release(struct inode *inode, struct file *file)
  381. {
  382. struct ceph_inode_info *ci = ceph_inode(inode);
  383. struct ceph_file_info *cf = file->private_data;
  384. dout("release inode %p file %p\n", inode, file);
  385. ceph_put_fmode(ci, cf->fmode);
  386. if (cf->last_readdir)
  387. ceph_mdsc_put_request(cf->last_readdir);
  388. kfree(cf->last_name);
  389. kfree(cf->dir_info);
  390. kmem_cache_free(ceph_file_cachep, cf);
  391. /* wake up anyone waiting for caps on this inode */
  392. wake_up_all(&ci->i_cap_wq);
  393. return 0;
  394. }
  395. enum {
  396. HAVE_RETRIED = 1,
  397. CHECK_EOF = 2,
  398. READ_INLINE = 3,
  399. };
  400. /*
  401. * Read a range of bytes striped over one or more objects. Iterate over
  402. * objects we stripe over. (That's not atomic, but good enough for now.)
  403. *
  404. * If we get a short result from the OSD, check against i_size; we need to
  405. * only return a short read to the caller if we hit EOF.
  406. */
  407. static int striped_read(struct inode *inode,
  408. u64 off, u64 len,
  409. struct page **pages, int num_pages,
  410. int *checkeof)
  411. {
  412. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  413. struct ceph_inode_info *ci = ceph_inode(inode);
  414. u64 pos, this_len, left;
  415. loff_t i_size;
  416. int page_align, pages_left;
  417. int read, ret;
  418. struct page **page_pos;
  419. bool hit_stripe, was_short;
  420. /*
  421. * we may need to do multiple reads. not atomic, unfortunately.
  422. */
  423. pos = off;
  424. left = len;
  425. page_pos = pages;
  426. pages_left = num_pages;
  427. read = 0;
  428. more:
  429. page_align = pos & ~PAGE_MASK;
  430. this_len = left;
  431. ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
  432. &ci->i_layout, pos, &this_len,
  433. ci->i_truncate_seq,
  434. ci->i_truncate_size,
  435. page_pos, pages_left, page_align);
  436. if (ret == -ENOENT)
  437. ret = 0;
  438. hit_stripe = this_len < left;
  439. was_short = ret >= 0 && ret < this_len;
  440. dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, left, read,
  441. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  442. i_size = i_size_read(inode);
  443. if (ret >= 0) {
  444. int didpages;
  445. if (was_short && (pos + ret < i_size)) {
  446. int zlen = min(this_len - ret, i_size - pos - ret);
  447. int zoff = (off & ~PAGE_MASK) + read + ret;
  448. dout(" zero gap %llu to %llu\n",
  449. pos + ret, pos + ret + zlen);
  450. ceph_zero_page_vector_range(zoff, zlen, pages);
  451. ret += zlen;
  452. }
  453. didpages = (page_align + ret) >> PAGE_SHIFT;
  454. pos += ret;
  455. read = pos - off;
  456. left -= ret;
  457. page_pos += didpages;
  458. pages_left -= didpages;
  459. /* hit stripe and need continue*/
  460. if (left && hit_stripe && pos < i_size)
  461. goto more;
  462. }
  463. if (read > 0) {
  464. ret = read;
  465. /* did we bounce off eof? */
  466. if (pos + left > i_size)
  467. *checkeof = CHECK_EOF;
  468. }
  469. dout("striped_read returns %d\n", ret);
  470. return ret;
  471. }
  472. /*
  473. * Completely synchronous read and write methods. Direct from __user
  474. * buffer to osd, or directly to user pages (if O_DIRECT).
  475. *
  476. * If the read spans object boundary, just do multiple reads.
  477. */
  478. static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *i,
  479. int *checkeof)
  480. {
  481. struct file *file = iocb->ki_filp;
  482. struct inode *inode = file_inode(file);
  483. struct page **pages;
  484. u64 off = iocb->ki_pos;
  485. int num_pages, ret;
  486. size_t len = iov_iter_count(i);
  487. dout("sync_read on file %p %llu~%u %s\n", file, off,
  488. (unsigned)len,
  489. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  490. if (!len)
  491. return 0;
  492. /*
  493. * flush any page cache pages in this range. this
  494. * will make concurrent normal and sync io slow,
  495. * but it will at least behave sensibly when they are
  496. * in sequence.
  497. */
  498. ret = filemap_write_and_wait_range(inode->i_mapping, off,
  499. off + len);
  500. if (ret < 0)
  501. return ret;
  502. num_pages = calc_pages_for(off, len);
  503. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  504. if (IS_ERR(pages))
  505. return PTR_ERR(pages);
  506. ret = striped_read(inode, off, len, pages,
  507. num_pages, checkeof);
  508. if (ret > 0) {
  509. int l, k = 0;
  510. size_t left = ret;
  511. while (left) {
  512. size_t page_off = off & ~PAGE_MASK;
  513. size_t copy = min_t(size_t, left,
  514. PAGE_SIZE - page_off);
  515. l = copy_page_to_iter(pages[k++], page_off, copy, i);
  516. off += l;
  517. left -= l;
  518. if (l < copy)
  519. break;
  520. }
  521. }
  522. ceph_release_page_vector(pages, num_pages);
  523. if (off > iocb->ki_pos) {
  524. ret = off - iocb->ki_pos;
  525. iocb->ki_pos = off;
  526. }
  527. dout("sync_read result %d\n", ret);
  528. return ret;
  529. }
  530. struct ceph_aio_request {
  531. struct kiocb *iocb;
  532. size_t total_len;
  533. int write;
  534. int error;
  535. struct list_head osd_reqs;
  536. unsigned num_reqs;
  537. atomic_t pending_reqs;
  538. struct timespec mtime;
  539. struct ceph_cap_flush *prealloc_cf;
  540. };
  541. struct ceph_aio_work {
  542. struct work_struct work;
  543. struct ceph_osd_request *req;
  544. };
  545. static void ceph_aio_retry_work(struct work_struct *work);
  546. static void ceph_aio_complete(struct inode *inode,
  547. struct ceph_aio_request *aio_req)
  548. {
  549. struct ceph_inode_info *ci = ceph_inode(inode);
  550. int ret;
  551. if (!atomic_dec_and_test(&aio_req->pending_reqs))
  552. return;
  553. ret = aio_req->error;
  554. if (!ret)
  555. ret = aio_req->total_len;
  556. dout("ceph_aio_complete %p rc %d\n", inode, ret);
  557. if (ret >= 0 && aio_req->write) {
  558. int dirty;
  559. loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
  560. if (endoff > i_size_read(inode)) {
  561. if (ceph_inode_set_size(inode, endoff))
  562. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  563. }
  564. spin_lock(&ci->i_ceph_lock);
  565. ci->i_inline_version = CEPH_INLINE_NONE;
  566. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  567. &aio_req->prealloc_cf);
  568. spin_unlock(&ci->i_ceph_lock);
  569. if (dirty)
  570. __mark_inode_dirty(inode, dirty);
  571. }
  572. ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
  573. CEPH_CAP_FILE_RD));
  574. aio_req->iocb->ki_complete(aio_req->iocb, ret, 0);
  575. ceph_free_cap_flush(aio_req->prealloc_cf);
  576. kfree(aio_req);
  577. }
  578. static void ceph_aio_complete_req(struct ceph_osd_request *req)
  579. {
  580. int rc = req->r_result;
  581. struct inode *inode = req->r_inode;
  582. struct ceph_aio_request *aio_req = req->r_priv;
  583. struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
  584. int num_pages = calc_pages_for((u64)osd_data->alignment,
  585. osd_data->length);
  586. dout("ceph_aio_complete_req %p rc %d bytes %llu\n",
  587. inode, rc, osd_data->length);
  588. if (rc == -EOLDSNAPC) {
  589. struct ceph_aio_work *aio_work;
  590. BUG_ON(!aio_req->write);
  591. aio_work = kmalloc(sizeof(*aio_work), GFP_NOFS);
  592. if (aio_work) {
  593. INIT_WORK(&aio_work->work, ceph_aio_retry_work);
  594. aio_work->req = req;
  595. queue_work(ceph_inode_to_client(inode)->wb_wq,
  596. &aio_work->work);
  597. return;
  598. }
  599. rc = -ENOMEM;
  600. } else if (!aio_req->write) {
  601. if (rc == -ENOENT)
  602. rc = 0;
  603. if (rc >= 0 && osd_data->length > rc) {
  604. int zoff = osd_data->alignment + rc;
  605. int zlen = osd_data->length - rc;
  606. /*
  607. * If read is satisfied by single OSD request,
  608. * it can pass EOF. Otherwise read is within
  609. * i_size.
  610. */
  611. if (aio_req->num_reqs == 1) {
  612. loff_t i_size = i_size_read(inode);
  613. loff_t endoff = aio_req->iocb->ki_pos + rc;
  614. if (endoff < i_size)
  615. zlen = min_t(size_t, zlen,
  616. i_size - endoff);
  617. aio_req->total_len = rc + zlen;
  618. }
  619. if (zlen > 0)
  620. ceph_zero_page_vector_range(zoff, zlen,
  621. osd_data->pages);
  622. }
  623. }
  624. ceph_put_page_vector(osd_data->pages, num_pages, !aio_req->write);
  625. ceph_osdc_put_request(req);
  626. if (rc < 0)
  627. cmpxchg(&aio_req->error, 0, rc);
  628. ceph_aio_complete(inode, aio_req);
  629. return;
  630. }
  631. static void ceph_aio_retry_work(struct work_struct *work)
  632. {
  633. struct ceph_aio_work *aio_work =
  634. container_of(work, struct ceph_aio_work, work);
  635. struct ceph_osd_request *orig_req = aio_work->req;
  636. struct ceph_aio_request *aio_req = orig_req->r_priv;
  637. struct inode *inode = orig_req->r_inode;
  638. struct ceph_inode_info *ci = ceph_inode(inode);
  639. struct ceph_snap_context *snapc;
  640. struct ceph_osd_request *req;
  641. int ret;
  642. spin_lock(&ci->i_ceph_lock);
  643. if (__ceph_have_pending_cap_snap(ci)) {
  644. struct ceph_cap_snap *capsnap =
  645. list_last_entry(&ci->i_cap_snaps,
  646. struct ceph_cap_snap,
  647. ci_item);
  648. snapc = ceph_get_snap_context(capsnap->context);
  649. } else {
  650. BUG_ON(!ci->i_head_snapc);
  651. snapc = ceph_get_snap_context(ci->i_head_snapc);
  652. }
  653. spin_unlock(&ci->i_ceph_lock);
  654. req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 2,
  655. false, GFP_NOFS);
  656. if (!req) {
  657. ret = -ENOMEM;
  658. req = orig_req;
  659. goto out;
  660. }
  661. req->r_flags = CEPH_OSD_FLAG_ORDERSNAP |
  662. CEPH_OSD_FLAG_ONDISK |
  663. CEPH_OSD_FLAG_WRITE;
  664. ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
  665. ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
  666. ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
  667. if (ret) {
  668. ceph_osdc_put_request(req);
  669. req = orig_req;
  670. goto out;
  671. }
  672. req->r_ops[0] = orig_req->r_ops[0];
  673. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC, 0);
  674. req->r_mtime = aio_req->mtime;
  675. req->r_data_offset = req->r_ops[0].extent.offset;
  676. ceph_osdc_put_request(orig_req);
  677. req->r_callback = ceph_aio_complete_req;
  678. req->r_inode = inode;
  679. req->r_priv = aio_req;
  680. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  681. out:
  682. if (ret < 0) {
  683. req->r_result = ret;
  684. ceph_aio_complete_req(req);
  685. }
  686. ceph_put_snap_context(snapc);
  687. kfree(aio_work);
  688. }
  689. /*
  690. * Write commit request unsafe callback, called to tell us when a
  691. * request is unsafe (that is, in flight--has been handed to the
  692. * messenger to send to its target osd). It is called again when
  693. * we've received a response message indicating the request is
  694. * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
  695. * is completed early (and unsuccessfully) due to a timeout or
  696. * interrupt.
  697. *
  698. * This is used if we requested both an ACK and ONDISK commit reply
  699. * from the OSD.
  700. */
  701. static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
  702. {
  703. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  704. dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
  705. unsafe ? "un" : "");
  706. if (unsafe) {
  707. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  708. spin_lock(&ci->i_unsafe_lock);
  709. list_add_tail(&req->r_unsafe_item,
  710. &ci->i_unsafe_writes);
  711. spin_unlock(&ci->i_unsafe_lock);
  712. complete_all(&req->r_completion);
  713. } else {
  714. spin_lock(&ci->i_unsafe_lock);
  715. list_del_init(&req->r_unsafe_item);
  716. spin_unlock(&ci->i_unsafe_lock);
  717. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  718. }
  719. }
  720. /*
  721. * Wait on any unsafe replies for the given inode. First wait on the
  722. * newest request, and make that the upper bound. Then, if there are
  723. * more requests, keep waiting on the oldest as long as it is still older
  724. * than the original request.
  725. */
  726. void ceph_sync_write_wait(struct inode *inode)
  727. {
  728. struct ceph_inode_info *ci = ceph_inode(inode);
  729. struct list_head *head = &ci->i_unsafe_writes;
  730. struct ceph_osd_request *req;
  731. u64 last_tid;
  732. if (!S_ISREG(inode->i_mode))
  733. return;
  734. spin_lock(&ci->i_unsafe_lock);
  735. if (list_empty(head))
  736. goto out;
  737. /* set upper bound as _last_ entry in chain */
  738. req = list_last_entry(head, struct ceph_osd_request,
  739. r_unsafe_item);
  740. last_tid = req->r_tid;
  741. do {
  742. ceph_osdc_get_request(req);
  743. spin_unlock(&ci->i_unsafe_lock);
  744. dout("sync_write_wait on tid %llu (until %llu)\n",
  745. req->r_tid, last_tid);
  746. wait_for_completion(&req->r_safe_completion);
  747. ceph_osdc_put_request(req);
  748. spin_lock(&ci->i_unsafe_lock);
  749. /*
  750. * from here on look at first entry in chain, since we
  751. * only want to wait for anything older than last_tid
  752. */
  753. if (list_empty(head))
  754. break;
  755. req = list_first_entry(head, struct ceph_osd_request,
  756. r_unsafe_item);
  757. } while (req->r_tid < last_tid);
  758. out:
  759. spin_unlock(&ci->i_unsafe_lock);
  760. }
  761. static ssize_t
  762. ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
  763. struct ceph_snap_context *snapc,
  764. struct ceph_cap_flush **pcf)
  765. {
  766. struct file *file = iocb->ki_filp;
  767. struct inode *inode = file_inode(file);
  768. struct ceph_inode_info *ci = ceph_inode(inode);
  769. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  770. struct ceph_vino vino;
  771. struct ceph_osd_request *req;
  772. struct page **pages;
  773. struct ceph_aio_request *aio_req = NULL;
  774. int num_pages = 0;
  775. int flags;
  776. int ret;
  777. struct timespec mtime = current_time(inode);
  778. size_t count = iov_iter_count(iter);
  779. loff_t pos = iocb->ki_pos;
  780. bool write = iov_iter_rw(iter) == WRITE;
  781. if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  782. return -EROFS;
  783. dout("sync_direct_read_write (%s) on file %p %lld~%u\n",
  784. (write ? "write" : "read"), file, pos, (unsigned)count);
  785. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  786. if (ret < 0)
  787. return ret;
  788. if (write) {
  789. int ret2 = invalidate_inode_pages2_range(inode->i_mapping,
  790. pos >> PAGE_SHIFT,
  791. (pos + count) >> PAGE_SHIFT);
  792. if (ret2 < 0)
  793. dout("invalidate_inode_pages2_range returned %d\n", ret);
  794. flags = CEPH_OSD_FLAG_ORDERSNAP |
  795. CEPH_OSD_FLAG_ONDISK |
  796. CEPH_OSD_FLAG_WRITE;
  797. } else {
  798. flags = CEPH_OSD_FLAG_READ;
  799. }
  800. while (iov_iter_count(iter) > 0) {
  801. u64 size = dio_get_pagev_size(iter);
  802. size_t start = 0;
  803. ssize_t len;
  804. vino = ceph_vino(inode);
  805. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  806. vino, pos, &size, 0,
  807. /*include a 'startsync' command*/
  808. write ? 2 : 1,
  809. write ? CEPH_OSD_OP_WRITE :
  810. CEPH_OSD_OP_READ,
  811. flags, snapc,
  812. ci->i_truncate_seq,
  813. ci->i_truncate_size,
  814. false);
  815. if (IS_ERR(req)) {
  816. ret = PTR_ERR(req);
  817. break;
  818. }
  819. len = size;
  820. pages = dio_get_pages_alloc(iter, len, &start, &num_pages);
  821. if (IS_ERR(pages)) {
  822. ceph_osdc_put_request(req);
  823. ret = PTR_ERR(pages);
  824. break;
  825. }
  826. /*
  827. * To simplify error handling, allow AIO when IO within i_size
  828. * or IO can be satisfied by single OSD request.
  829. */
  830. if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
  831. (len == count || pos + count <= i_size_read(inode))) {
  832. aio_req = kzalloc(sizeof(*aio_req), GFP_KERNEL);
  833. if (aio_req) {
  834. aio_req->iocb = iocb;
  835. aio_req->write = write;
  836. INIT_LIST_HEAD(&aio_req->osd_reqs);
  837. if (write) {
  838. aio_req->mtime = mtime;
  839. swap(aio_req->prealloc_cf, *pcf);
  840. }
  841. }
  842. /* ignore error */
  843. }
  844. if (write) {
  845. /*
  846. * throw out any page cache pages in this range. this
  847. * may block.
  848. */
  849. truncate_inode_pages_range(inode->i_mapping, pos,
  850. (pos+len) | (PAGE_SIZE - 1));
  851. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC, 0);
  852. req->r_mtime = mtime;
  853. }
  854. osd_req_op_extent_osd_data_pages(req, 0, pages, len, start,
  855. false, false);
  856. if (aio_req) {
  857. aio_req->total_len += len;
  858. aio_req->num_reqs++;
  859. atomic_inc(&aio_req->pending_reqs);
  860. req->r_callback = ceph_aio_complete_req;
  861. req->r_inode = inode;
  862. req->r_priv = aio_req;
  863. list_add_tail(&req->r_unsafe_item, &aio_req->osd_reqs);
  864. pos += len;
  865. iov_iter_advance(iter, len);
  866. continue;
  867. }
  868. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  869. if (!ret)
  870. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  871. size = i_size_read(inode);
  872. if (!write) {
  873. if (ret == -ENOENT)
  874. ret = 0;
  875. if (ret >= 0 && ret < len && pos + ret < size) {
  876. int zlen = min_t(size_t, len - ret,
  877. size - pos - ret);
  878. ceph_zero_page_vector_range(start + ret, zlen,
  879. pages);
  880. ret += zlen;
  881. }
  882. if (ret >= 0)
  883. len = ret;
  884. }
  885. ceph_put_page_vector(pages, num_pages, !write);
  886. ceph_osdc_put_request(req);
  887. if (ret < 0)
  888. break;
  889. pos += len;
  890. iov_iter_advance(iter, len);
  891. if (!write && pos >= size)
  892. break;
  893. if (write && pos > size) {
  894. if (ceph_inode_set_size(inode, pos))
  895. ceph_check_caps(ceph_inode(inode),
  896. CHECK_CAPS_AUTHONLY,
  897. NULL);
  898. }
  899. }
  900. if (aio_req) {
  901. LIST_HEAD(osd_reqs);
  902. if (aio_req->num_reqs == 0) {
  903. kfree(aio_req);
  904. return ret;
  905. }
  906. ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
  907. CEPH_CAP_FILE_RD);
  908. list_splice(&aio_req->osd_reqs, &osd_reqs);
  909. while (!list_empty(&osd_reqs)) {
  910. req = list_first_entry(&osd_reqs,
  911. struct ceph_osd_request,
  912. r_unsafe_item);
  913. list_del_init(&req->r_unsafe_item);
  914. if (ret >= 0)
  915. ret = ceph_osdc_start_request(req->r_osdc,
  916. req, false);
  917. if (ret < 0) {
  918. req->r_result = ret;
  919. ceph_aio_complete_req(req);
  920. }
  921. }
  922. return -EIOCBQUEUED;
  923. }
  924. if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
  925. ret = pos - iocb->ki_pos;
  926. iocb->ki_pos = pos;
  927. }
  928. return ret;
  929. }
  930. /*
  931. * Synchronous write, straight from __user pointer or user pages.
  932. *
  933. * If write spans object boundary, just do multiple writes. (For a
  934. * correct atomic write, we should e.g. take write locks on all
  935. * objects, rollback on failure, etc.)
  936. */
  937. static ssize_t
  938. ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
  939. struct ceph_snap_context *snapc)
  940. {
  941. struct file *file = iocb->ki_filp;
  942. struct inode *inode = file_inode(file);
  943. struct ceph_inode_info *ci = ceph_inode(inode);
  944. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  945. struct ceph_vino vino;
  946. struct ceph_osd_request *req;
  947. struct page **pages;
  948. u64 len;
  949. int num_pages;
  950. int written = 0;
  951. int flags;
  952. int check_caps = 0;
  953. int ret;
  954. struct timespec mtime = current_time(inode);
  955. size_t count = iov_iter_count(from);
  956. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  957. return -EROFS;
  958. dout("sync_write on file %p %lld~%u\n", file, pos, (unsigned)count);
  959. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  960. if (ret < 0)
  961. return ret;
  962. ret = invalidate_inode_pages2_range(inode->i_mapping,
  963. pos >> PAGE_SHIFT,
  964. (pos + count) >> PAGE_SHIFT);
  965. if (ret < 0)
  966. dout("invalidate_inode_pages2_range returned %d\n", ret);
  967. flags = CEPH_OSD_FLAG_ORDERSNAP |
  968. CEPH_OSD_FLAG_ONDISK |
  969. CEPH_OSD_FLAG_WRITE |
  970. CEPH_OSD_FLAG_ACK;
  971. while ((len = iov_iter_count(from)) > 0) {
  972. size_t left;
  973. int n;
  974. vino = ceph_vino(inode);
  975. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  976. vino, pos, &len, 0, 1,
  977. CEPH_OSD_OP_WRITE, flags, snapc,
  978. ci->i_truncate_seq,
  979. ci->i_truncate_size,
  980. false);
  981. if (IS_ERR(req)) {
  982. ret = PTR_ERR(req);
  983. break;
  984. }
  985. /*
  986. * write from beginning of first page,
  987. * regardless of io alignment
  988. */
  989. num_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  990. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  991. if (IS_ERR(pages)) {
  992. ret = PTR_ERR(pages);
  993. goto out;
  994. }
  995. left = len;
  996. for (n = 0; n < num_pages; n++) {
  997. size_t plen = min_t(size_t, left, PAGE_SIZE);
  998. ret = copy_page_from_iter(pages[n], 0, plen, from);
  999. if (ret != plen) {
  1000. ret = -EFAULT;
  1001. break;
  1002. }
  1003. left -= ret;
  1004. }
  1005. if (ret < 0) {
  1006. ceph_release_page_vector(pages, num_pages);
  1007. goto out;
  1008. }
  1009. /* get a second commit callback */
  1010. req->r_unsafe_callback = ceph_sync_write_unsafe;
  1011. req->r_inode = inode;
  1012. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
  1013. false, true);
  1014. req->r_mtime = mtime;
  1015. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1016. if (!ret)
  1017. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1018. out:
  1019. ceph_osdc_put_request(req);
  1020. if (ret == 0) {
  1021. pos += len;
  1022. written += len;
  1023. if (pos > i_size_read(inode)) {
  1024. check_caps = ceph_inode_set_size(inode, pos);
  1025. if (check_caps)
  1026. ceph_check_caps(ceph_inode(inode),
  1027. CHECK_CAPS_AUTHONLY,
  1028. NULL);
  1029. }
  1030. } else
  1031. break;
  1032. }
  1033. if (ret != -EOLDSNAPC && written > 0) {
  1034. ret = written;
  1035. iocb->ki_pos = pos;
  1036. }
  1037. return ret;
  1038. }
  1039. /*
  1040. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  1041. * Atomically grab references, so that those bits are not released
  1042. * back to the MDS mid-read.
  1043. *
  1044. * Hmm, the sync read case isn't actually async... should it be?
  1045. */
  1046. static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1047. {
  1048. struct file *filp = iocb->ki_filp;
  1049. struct ceph_file_info *fi = filp->private_data;
  1050. size_t len = iov_iter_count(to);
  1051. struct inode *inode = file_inode(filp);
  1052. struct ceph_inode_info *ci = ceph_inode(inode);
  1053. struct page *pinned_page = NULL;
  1054. ssize_t ret;
  1055. int want, got = 0;
  1056. int retry_op = 0, read = 0;
  1057. again:
  1058. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  1059. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
  1060. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1061. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  1062. else
  1063. want = CEPH_CAP_FILE_CACHE;
  1064. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
  1065. if (ret < 0)
  1066. return ret;
  1067. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  1068. (iocb->ki_flags & IOCB_DIRECT) ||
  1069. (fi->flags & CEPH_F_SYNC)) {
  1070. dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  1071. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1072. ceph_cap_string(got));
  1073. if (ci->i_inline_version == CEPH_INLINE_NONE) {
  1074. if (!retry_op && (iocb->ki_flags & IOCB_DIRECT)) {
  1075. ret = ceph_direct_read_write(iocb, to,
  1076. NULL, NULL);
  1077. if (ret >= 0 && ret < len)
  1078. retry_op = CHECK_EOF;
  1079. } else {
  1080. ret = ceph_sync_read(iocb, to, &retry_op);
  1081. }
  1082. } else {
  1083. retry_op = READ_INLINE;
  1084. }
  1085. } else {
  1086. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  1087. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1088. ceph_cap_string(got));
  1089. current->journal_info = filp;
  1090. ret = generic_file_read_iter(iocb, to);
  1091. current->journal_info = NULL;
  1092. }
  1093. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  1094. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  1095. if (pinned_page) {
  1096. put_page(pinned_page);
  1097. pinned_page = NULL;
  1098. }
  1099. ceph_put_cap_refs(ci, got);
  1100. if (retry_op > HAVE_RETRIED && ret >= 0) {
  1101. int statret;
  1102. struct page *page = NULL;
  1103. loff_t i_size;
  1104. if (retry_op == READ_INLINE) {
  1105. page = __page_cache_alloc(GFP_KERNEL);
  1106. if (!page)
  1107. return -ENOMEM;
  1108. }
  1109. statret = __ceph_do_getattr(inode, page,
  1110. CEPH_STAT_CAP_INLINE_DATA, !!page);
  1111. if (statret < 0) {
  1112. if (page)
  1113. __free_page(page);
  1114. if (statret == -ENODATA) {
  1115. BUG_ON(retry_op != READ_INLINE);
  1116. goto again;
  1117. }
  1118. return statret;
  1119. }
  1120. i_size = i_size_read(inode);
  1121. if (retry_op == READ_INLINE) {
  1122. BUG_ON(ret > 0 || read > 0);
  1123. if (iocb->ki_pos < i_size &&
  1124. iocb->ki_pos < PAGE_SIZE) {
  1125. loff_t end = min_t(loff_t, i_size,
  1126. iocb->ki_pos + len);
  1127. end = min_t(loff_t, end, PAGE_SIZE);
  1128. if (statret < end)
  1129. zero_user_segment(page, statret, end);
  1130. ret = copy_page_to_iter(page,
  1131. iocb->ki_pos & ~PAGE_MASK,
  1132. end - iocb->ki_pos, to);
  1133. iocb->ki_pos += ret;
  1134. read += ret;
  1135. }
  1136. if (iocb->ki_pos < i_size && read < len) {
  1137. size_t zlen = min_t(size_t, len - read,
  1138. i_size - iocb->ki_pos);
  1139. ret = iov_iter_zero(zlen, to);
  1140. iocb->ki_pos += ret;
  1141. read += ret;
  1142. }
  1143. __free_pages(page, 0);
  1144. return read;
  1145. }
  1146. /* hit EOF or hole? */
  1147. if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
  1148. ret < len) {
  1149. dout("sync_read hit hole, ppos %lld < size %lld"
  1150. ", reading more\n", iocb->ki_pos, i_size);
  1151. read += ret;
  1152. len -= ret;
  1153. retry_op = HAVE_RETRIED;
  1154. goto again;
  1155. }
  1156. }
  1157. if (ret >= 0)
  1158. ret += read;
  1159. return ret;
  1160. }
  1161. /*
  1162. * Take cap references to avoid releasing caps to MDS mid-write.
  1163. *
  1164. * If we are synchronous, and write with an old snap context, the OSD
  1165. * may return EOLDSNAPC. In that case, retry the write.. _after_
  1166. * dropping our cap refs and allowing the pending snap to logically
  1167. * complete _before_ this write occurs.
  1168. *
  1169. * If we are near ENOSPC, write synchronously.
  1170. */
  1171. static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1172. {
  1173. struct file *file = iocb->ki_filp;
  1174. struct ceph_file_info *fi = file->private_data;
  1175. struct inode *inode = file_inode(file);
  1176. struct ceph_inode_info *ci = ceph_inode(inode);
  1177. struct ceph_osd_client *osdc =
  1178. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  1179. struct ceph_cap_flush *prealloc_cf;
  1180. ssize_t count, written = 0;
  1181. int err, want, got;
  1182. loff_t pos;
  1183. if (ceph_snap(inode) != CEPH_NOSNAP)
  1184. return -EROFS;
  1185. prealloc_cf = ceph_alloc_cap_flush();
  1186. if (!prealloc_cf)
  1187. return -ENOMEM;
  1188. inode_lock(inode);
  1189. /* We can write back this queue in page reclaim */
  1190. current->backing_dev_info = inode_to_bdi(inode);
  1191. if (iocb->ki_flags & IOCB_APPEND) {
  1192. err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1193. if (err < 0)
  1194. goto out;
  1195. }
  1196. err = generic_write_checks(iocb, from);
  1197. if (err <= 0)
  1198. goto out;
  1199. pos = iocb->ki_pos;
  1200. count = iov_iter_count(from);
  1201. err = file_remove_privs(file);
  1202. if (err)
  1203. goto out;
  1204. err = file_update_time(file);
  1205. if (err)
  1206. goto out;
  1207. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1208. err = ceph_uninline_data(file, NULL);
  1209. if (err < 0)
  1210. goto out;
  1211. }
  1212. retry_snap:
  1213. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL)) {
  1214. err = -ENOSPC;
  1215. goto out;
  1216. }
  1217. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  1218. inode, ceph_vinop(inode), pos, count, i_size_read(inode));
  1219. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1220. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1221. else
  1222. want = CEPH_CAP_FILE_BUFFER;
  1223. got = 0;
  1224. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, pos + count,
  1225. &got, NULL);
  1226. if (err < 0)
  1227. goto out;
  1228. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  1229. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  1230. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  1231. (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC)) {
  1232. struct ceph_snap_context *snapc;
  1233. struct iov_iter data;
  1234. inode_unlock(inode);
  1235. spin_lock(&ci->i_ceph_lock);
  1236. if (__ceph_have_pending_cap_snap(ci)) {
  1237. struct ceph_cap_snap *capsnap =
  1238. list_last_entry(&ci->i_cap_snaps,
  1239. struct ceph_cap_snap,
  1240. ci_item);
  1241. snapc = ceph_get_snap_context(capsnap->context);
  1242. } else {
  1243. BUG_ON(!ci->i_head_snapc);
  1244. snapc = ceph_get_snap_context(ci->i_head_snapc);
  1245. }
  1246. spin_unlock(&ci->i_ceph_lock);
  1247. /* we might need to revert back to that point */
  1248. data = *from;
  1249. if (iocb->ki_flags & IOCB_DIRECT)
  1250. written = ceph_direct_read_write(iocb, &data, snapc,
  1251. &prealloc_cf);
  1252. else
  1253. written = ceph_sync_write(iocb, &data, pos, snapc);
  1254. if (written == -EOLDSNAPC) {
  1255. dout("aio_write %p %llx.%llx %llu~%u"
  1256. "got EOLDSNAPC, retrying\n",
  1257. inode, ceph_vinop(inode),
  1258. pos, (unsigned)count);
  1259. inode_lock(inode);
  1260. goto retry_snap;
  1261. }
  1262. if (written > 0)
  1263. iov_iter_advance(from, written);
  1264. ceph_put_snap_context(snapc);
  1265. } else {
  1266. /*
  1267. * No need to acquire the i_truncate_mutex. Because
  1268. * the MDS revokes Fwb caps before sending truncate
  1269. * message to us. We can't get Fwb cap while there
  1270. * are pending vmtruncate. So write and vmtruncate
  1271. * can not run at the same time
  1272. */
  1273. written = generic_perform_write(file, from, pos);
  1274. if (likely(written >= 0))
  1275. iocb->ki_pos = pos + written;
  1276. inode_unlock(inode);
  1277. }
  1278. if (written >= 0) {
  1279. int dirty;
  1280. spin_lock(&ci->i_ceph_lock);
  1281. ci->i_inline_version = CEPH_INLINE_NONE;
  1282. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1283. &prealloc_cf);
  1284. spin_unlock(&ci->i_ceph_lock);
  1285. if (dirty)
  1286. __mark_inode_dirty(inode, dirty);
  1287. }
  1288. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  1289. inode, ceph_vinop(inode), pos, (unsigned)count,
  1290. ceph_cap_string(got));
  1291. ceph_put_cap_refs(ci, got);
  1292. if (written >= 0) {
  1293. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_NEARFULL))
  1294. iocb->ki_flags |= IOCB_DSYNC;
  1295. written = generic_write_sync(iocb, written);
  1296. }
  1297. goto out_unlocked;
  1298. out:
  1299. inode_unlock(inode);
  1300. out_unlocked:
  1301. ceph_free_cap_flush(prealloc_cf);
  1302. current->backing_dev_info = NULL;
  1303. return written ? written : err;
  1304. }
  1305. /*
  1306. * llseek. be sure to verify file size on SEEK_END.
  1307. */
  1308. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  1309. {
  1310. struct inode *inode = file->f_mapping->host;
  1311. loff_t i_size;
  1312. loff_t ret;
  1313. inode_lock(inode);
  1314. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  1315. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1316. if (ret < 0)
  1317. goto out;
  1318. }
  1319. i_size = i_size_read(inode);
  1320. switch (whence) {
  1321. case SEEK_END:
  1322. offset += i_size;
  1323. break;
  1324. case SEEK_CUR:
  1325. /*
  1326. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  1327. * position-querying operation. Avoid rewriting the "same"
  1328. * f_pos value back to the file because a concurrent read(),
  1329. * write() or lseek() might have altered it
  1330. */
  1331. if (offset == 0) {
  1332. ret = file->f_pos;
  1333. goto out;
  1334. }
  1335. offset += file->f_pos;
  1336. break;
  1337. case SEEK_DATA:
  1338. if (offset >= i_size) {
  1339. ret = -ENXIO;
  1340. goto out;
  1341. }
  1342. break;
  1343. case SEEK_HOLE:
  1344. if (offset >= i_size) {
  1345. ret = -ENXIO;
  1346. goto out;
  1347. }
  1348. offset = i_size;
  1349. break;
  1350. }
  1351. ret = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  1352. out:
  1353. inode_unlock(inode);
  1354. return ret;
  1355. }
  1356. static inline void ceph_zero_partial_page(
  1357. struct inode *inode, loff_t offset, unsigned size)
  1358. {
  1359. struct page *page;
  1360. pgoff_t index = offset >> PAGE_SHIFT;
  1361. page = find_lock_page(inode->i_mapping, index);
  1362. if (page) {
  1363. wait_on_page_writeback(page);
  1364. zero_user(page, offset & (PAGE_SIZE - 1), size);
  1365. unlock_page(page);
  1366. put_page(page);
  1367. }
  1368. }
  1369. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  1370. loff_t length)
  1371. {
  1372. loff_t nearly = round_up(offset, PAGE_SIZE);
  1373. if (offset < nearly) {
  1374. loff_t size = nearly - offset;
  1375. if (length < size)
  1376. size = length;
  1377. ceph_zero_partial_page(inode, offset, size);
  1378. offset += size;
  1379. length -= size;
  1380. }
  1381. if (length >= PAGE_SIZE) {
  1382. loff_t size = round_down(length, PAGE_SIZE);
  1383. truncate_pagecache_range(inode, offset, offset + size - 1);
  1384. offset += size;
  1385. length -= size;
  1386. }
  1387. if (length)
  1388. ceph_zero_partial_page(inode, offset, length);
  1389. }
  1390. static int ceph_zero_partial_object(struct inode *inode,
  1391. loff_t offset, loff_t *length)
  1392. {
  1393. struct ceph_inode_info *ci = ceph_inode(inode);
  1394. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1395. struct ceph_osd_request *req;
  1396. int ret = 0;
  1397. loff_t zero = 0;
  1398. int op;
  1399. if (!length) {
  1400. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  1401. length = &zero;
  1402. } else {
  1403. op = CEPH_OSD_OP_ZERO;
  1404. }
  1405. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1406. ceph_vino(inode),
  1407. offset, length,
  1408. 0, 1, op,
  1409. CEPH_OSD_FLAG_WRITE |
  1410. CEPH_OSD_FLAG_ONDISK,
  1411. NULL, 0, 0, false);
  1412. if (IS_ERR(req)) {
  1413. ret = PTR_ERR(req);
  1414. goto out;
  1415. }
  1416. req->r_mtime = inode->i_mtime;
  1417. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1418. if (!ret) {
  1419. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1420. if (ret == -ENOENT)
  1421. ret = 0;
  1422. }
  1423. ceph_osdc_put_request(req);
  1424. out:
  1425. return ret;
  1426. }
  1427. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  1428. {
  1429. int ret = 0;
  1430. struct ceph_inode_info *ci = ceph_inode(inode);
  1431. s32 stripe_unit = ci->i_layout.stripe_unit;
  1432. s32 stripe_count = ci->i_layout.stripe_count;
  1433. s32 object_size = ci->i_layout.object_size;
  1434. u64 object_set_size = object_size * stripe_count;
  1435. u64 nearly, t;
  1436. /* round offset up to next period boundary */
  1437. nearly = offset + object_set_size - 1;
  1438. t = nearly;
  1439. nearly -= do_div(t, object_set_size);
  1440. while (length && offset < nearly) {
  1441. loff_t size = length;
  1442. ret = ceph_zero_partial_object(inode, offset, &size);
  1443. if (ret < 0)
  1444. return ret;
  1445. offset += size;
  1446. length -= size;
  1447. }
  1448. while (length >= object_set_size) {
  1449. int i;
  1450. loff_t pos = offset;
  1451. for (i = 0; i < stripe_count; ++i) {
  1452. ret = ceph_zero_partial_object(inode, pos, NULL);
  1453. if (ret < 0)
  1454. return ret;
  1455. pos += stripe_unit;
  1456. }
  1457. offset += object_set_size;
  1458. length -= object_set_size;
  1459. }
  1460. while (length) {
  1461. loff_t size = length;
  1462. ret = ceph_zero_partial_object(inode, offset, &size);
  1463. if (ret < 0)
  1464. return ret;
  1465. offset += size;
  1466. length -= size;
  1467. }
  1468. return ret;
  1469. }
  1470. static long ceph_fallocate(struct file *file, int mode,
  1471. loff_t offset, loff_t length)
  1472. {
  1473. struct ceph_file_info *fi = file->private_data;
  1474. struct inode *inode = file_inode(file);
  1475. struct ceph_inode_info *ci = ceph_inode(inode);
  1476. struct ceph_osd_client *osdc =
  1477. &ceph_inode_to_client(inode)->client->osdc;
  1478. struct ceph_cap_flush *prealloc_cf;
  1479. int want, got = 0;
  1480. int dirty;
  1481. int ret = 0;
  1482. loff_t endoff = 0;
  1483. loff_t size;
  1484. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1485. return -EOPNOTSUPP;
  1486. if (!S_ISREG(inode->i_mode))
  1487. return -EOPNOTSUPP;
  1488. prealloc_cf = ceph_alloc_cap_flush();
  1489. if (!prealloc_cf)
  1490. return -ENOMEM;
  1491. inode_lock(inode);
  1492. if (ceph_snap(inode) != CEPH_NOSNAP) {
  1493. ret = -EROFS;
  1494. goto unlock;
  1495. }
  1496. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) &&
  1497. !(mode & FALLOC_FL_PUNCH_HOLE)) {
  1498. ret = -ENOSPC;
  1499. goto unlock;
  1500. }
  1501. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1502. ret = ceph_uninline_data(file, NULL);
  1503. if (ret < 0)
  1504. goto unlock;
  1505. }
  1506. size = i_size_read(inode);
  1507. if (!(mode & FALLOC_FL_KEEP_SIZE))
  1508. endoff = offset + length;
  1509. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1510. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1511. else
  1512. want = CEPH_CAP_FILE_BUFFER;
  1513. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, endoff, &got, NULL);
  1514. if (ret < 0)
  1515. goto unlock;
  1516. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1517. if (offset < size)
  1518. ceph_zero_pagecache_range(inode, offset, length);
  1519. ret = ceph_zero_objects(inode, offset, length);
  1520. } else if (endoff > size) {
  1521. truncate_pagecache_range(inode, size, -1);
  1522. if (ceph_inode_set_size(inode, endoff))
  1523. ceph_check_caps(ceph_inode(inode),
  1524. CHECK_CAPS_AUTHONLY, NULL);
  1525. }
  1526. if (!ret) {
  1527. spin_lock(&ci->i_ceph_lock);
  1528. ci->i_inline_version = CEPH_INLINE_NONE;
  1529. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1530. &prealloc_cf);
  1531. spin_unlock(&ci->i_ceph_lock);
  1532. if (dirty)
  1533. __mark_inode_dirty(inode, dirty);
  1534. }
  1535. ceph_put_cap_refs(ci, got);
  1536. unlock:
  1537. inode_unlock(inode);
  1538. ceph_free_cap_flush(prealloc_cf);
  1539. return ret;
  1540. }
  1541. const struct file_operations ceph_file_fops = {
  1542. .open = ceph_open,
  1543. .release = ceph_release,
  1544. .llseek = ceph_llseek,
  1545. .read_iter = ceph_read_iter,
  1546. .write_iter = ceph_write_iter,
  1547. .mmap = ceph_mmap,
  1548. .fsync = ceph_fsync,
  1549. .lock = ceph_lock,
  1550. .flock = ceph_flock,
  1551. .splice_write = iter_file_splice_write,
  1552. .unlocked_ioctl = ceph_ioctl,
  1553. .compat_ioctl = ceph_ioctl,
  1554. .fallocate = ceph_fallocate,
  1555. };