file.c 44 KB

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