file.c 45 KB

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