file.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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_alloc(ceph_file_cachep, GFP_KERNEL | __GFP_ZERO);
  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. * If we already have the requisite capabilities, we can satisfy
  177. * the open request locally (no need to request new caps from the
  178. * MDS). We do, however, need to inform the MDS (asynchronously)
  179. * if our wanted caps set expands.
  180. */
  181. int ceph_open(struct inode *inode, struct file *file)
  182. {
  183. struct ceph_inode_info *ci = ceph_inode(inode);
  184. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  185. struct ceph_mds_client *mdsc = fsc->mdsc;
  186. struct ceph_mds_request *req;
  187. struct ceph_file_info *cf = file->private_data;
  188. int err;
  189. int flags, fmode, wanted;
  190. if (cf) {
  191. dout("open file %p is already opened\n", file);
  192. return 0;
  193. }
  194. /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
  195. flags = file->f_flags & ~(O_CREAT|O_EXCL);
  196. if (S_ISDIR(inode->i_mode))
  197. flags = O_DIRECTORY; /* mds likes to know */
  198. dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
  199. ceph_vinop(inode), file, flags, file->f_flags);
  200. fmode = ceph_flags_to_mode(flags);
  201. wanted = ceph_caps_for_mode(fmode);
  202. /* snapped files are read-only */
  203. if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
  204. return -EROFS;
  205. /* trivially open snapdir */
  206. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  207. spin_lock(&ci->i_ceph_lock);
  208. __ceph_get_fmode(ci, fmode);
  209. spin_unlock(&ci->i_ceph_lock);
  210. return ceph_init_file(inode, file, fmode);
  211. }
  212. /*
  213. * No need to block if we have caps on the auth MDS (for
  214. * write) or any MDS (for read). Update wanted set
  215. * asynchronously.
  216. */
  217. spin_lock(&ci->i_ceph_lock);
  218. if (__ceph_is_any_real_caps(ci) &&
  219. (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
  220. int mds_wanted = __ceph_caps_mds_wanted(ci);
  221. int issued = __ceph_caps_issued(ci, NULL);
  222. dout("open %p fmode %d want %s issued %s using existing\n",
  223. inode, fmode, ceph_cap_string(wanted),
  224. ceph_cap_string(issued));
  225. __ceph_get_fmode(ci, fmode);
  226. spin_unlock(&ci->i_ceph_lock);
  227. /* adjust wanted? */
  228. if ((issued & wanted) != wanted &&
  229. (mds_wanted & wanted) != wanted &&
  230. ceph_snap(inode) != CEPH_SNAPDIR)
  231. ceph_check_caps(ci, 0, NULL);
  232. return ceph_init_file(inode, file, fmode);
  233. } else if (ceph_snap(inode) != CEPH_NOSNAP &&
  234. (ci->i_snap_caps & wanted) == wanted) {
  235. __ceph_get_fmode(ci, fmode);
  236. spin_unlock(&ci->i_ceph_lock);
  237. return ceph_init_file(inode, file, fmode);
  238. }
  239. spin_unlock(&ci->i_ceph_lock);
  240. dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
  241. req = prepare_open_request(inode->i_sb, flags, 0);
  242. if (IS_ERR(req)) {
  243. err = PTR_ERR(req);
  244. goto out;
  245. }
  246. req->r_inode = inode;
  247. ihold(inode);
  248. req->r_num_caps = 1;
  249. err = ceph_mdsc_do_request(mdsc, NULL, req);
  250. if (!err)
  251. err = ceph_init_file(inode, file, req->r_fmode);
  252. ceph_mdsc_put_request(req);
  253. dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
  254. out:
  255. return err;
  256. }
  257. /*
  258. * Do a lookup + open with a single request. If we get a non-existent
  259. * file or symlink, return 1 so the VFS can retry.
  260. */
  261. int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
  262. struct file *file, unsigned flags, umode_t mode,
  263. int *opened)
  264. {
  265. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  266. struct ceph_mds_client *mdsc = fsc->mdsc;
  267. struct ceph_mds_request *req;
  268. struct dentry *dn;
  269. struct ceph_acls_info acls = {};
  270. int err;
  271. dout("atomic_open %p dentry %p '%pd' %s flags %d mode 0%o\n",
  272. dir, dentry, dentry,
  273. d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
  274. if (dentry->d_name.len > NAME_MAX)
  275. return -ENAMETOOLONG;
  276. err = ceph_init_dentry(dentry);
  277. if (err < 0)
  278. return err;
  279. if (flags & O_CREAT) {
  280. err = ceph_pre_init_acls(dir, &mode, &acls);
  281. if (err < 0)
  282. return err;
  283. }
  284. /* do the open */
  285. req = prepare_open_request(dir->i_sb, flags, mode);
  286. if (IS_ERR(req)) {
  287. err = PTR_ERR(req);
  288. goto out_acl;
  289. }
  290. req->r_dentry = dget(dentry);
  291. req->r_num_caps = 2;
  292. if (flags & O_CREAT) {
  293. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  294. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  295. if (acls.pagelist) {
  296. req->r_pagelist = acls.pagelist;
  297. acls.pagelist = NULL;
  298. }
  299. }
  300. req->r_locked_dir = dir; /* caller holds dir->i_mutex */
  301. err = ceph_mdsc_do_request(mdsc,
  302. (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
  303. req);
  304. err = ceph_handle_snapdir(req, dentry, err);
  305. if (err)
  306. goto out_req;
  307. if ((flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  308. err = ceph_handle_notrace_create(dir, dentry);
  309. if (d_unhashed(dentry)) {
  310. dn = ceph_finish_lookup(req, dentry, err);
  311. if (IS_ERR(dn))
  312. err = PTR_ERR(dn);
  313. } else {
  314. /* we were given a hashed negative dentry */
  315. dn = NULL;
  316. }
  317. if (err)
  318. goto out_req;
  319. if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
  320. /* make vfs retry on splice, ENOENT, or symlink */
  321. dout("atomic_open finish_no_open on dn %p\n", dn);
  322. err = finish_no_open(file, dn);
  323. } else {
  324. dout("atomic_open finish_open on dn %p\n", dn);
  325. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  326. ceph_init_inode_acls(d_inode(dentry), &acls);
  327. *opened |= FILE_CREATED;
  328. }
  329. err = finish_open(file, dentry, ceph_open, opened);
  330. }
  331. out_req:
  332. if (!req->r_err && req->r_target_inode)
  333. ceph_put_fmode(ceph_inode(req->r_target_inode), req->r_fmode);
  334. ceph_mdsc_put_request(req);
  335. out_acl:
  336. ceph_release_acls_info(&acls);
  337. dout("atomic_open result=%d\n", err);
  338. return err;
  339. }
  340. int ceph_release(struct inode *inode, struct file *file)
  341. {
  342. struct ceph_inode_info *ci = ceph_inode(inode);
  343. struct ceph_file_info *cf = file->private_data;
  344. dout("release inode %p file %p\n", inode, file);
  345. ceph_put_fmode(ci, cf->fmode);
  346. if (cf->last_readdir)
  347. ceph_mdsc_put_request(cf->last_readdir);
  348. kfree(cf->last_name);
  349. kfree(cf->dir_info);
  350. kmem_cache_free(ceph_file_cachep, cf);
  351. /* wake up anyone waiting for caps on this inode */
  352. wake_up_all(&ci->i_cap_wq);
  353. return 0;
  354. }
  355. enum {
  356. CHECK_EOF = 1,
  357. READ_INLINE = 2,
  358. };
  359. /*
  360. * Read a range of bytes striped over one or more objects. Iterate over
  361. * objects we stripe over. (That's not atomic, but good enough for now.)
  362. *
  363. * If we get a short result from the OSD, check against i_size; we need to
  364. * only return a short read to the caller if we hit EOF.
  365. */
  366. static int striped_read(struct inode *inode,
  367. u64 off, u64 len,
  368. struct page **pages, int num_pages,
  369. int *checkeof, bool o_direct,
  370. unsigned long buf_align)
  371. {
  372. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  373. struct ceph_inode_info *ci = ceph_inode(inode);
  374. u64 pos, this_len, left;
  375. int io_align, page_align;
  376. int pages_left;
  377. int read;
  378. struct page **page_pos;
  379. int ret;
  380. bool hit_stripe, was_short;
  381. /*
  382. * we may need to do multiple reads. not atomic, unfortunately.
  383. */
  384. pos = off;
  385. left = len;
  386. page_pos = pages;
  387. pages_left = num_pages;
  388. read = 0;
  389. io_align = off & ~PAGE_MASK;
  390. more:
  391. if (o_direct)
  392. page_align = (pos - io_align + buf_align) & ~PAGE_MASK;
  393. else
  394. page_align = pos & ~PAGE_MASK;
  395. this_len = left;
  396. ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
  397. &ci->i_layout, pos, &this_len,
  398. ci->i_truncate_seq,
  399. ci->i_truncate_size,
  400. page_pos, pages_left, page_align);
  401. if (ret == -ENOENT)
  402. ret = 0;
  403. hit_stripe = this_len < left;
  404. was_short = ret >= 0 && ret < this_len;
  405. dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, left, read,
  406. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  407. if (ret >= 0) {
  408. int didpages;
  409. if (was_short && (pos + ret < inode->i_size)) {
  410. int zlen = min(this_len - ret,
  411. inode->i_size - pos - ret);
  412. int zoff = (o_direct ? buf_align : io_align) +
  413. read + ret;
  414. dout(" zero gap %llu to %llu\n",
  415. pos + ret, pos + ret + zlen);
  416. ceph_zero_page_vector_range(zoff, zlen, pages);
  417. ret += zlen;
  418. }
  419. didpages = (page_align + ret) >> PAGE_CACHE_SHIFT;
  420. pos += ret;
  421. read = pos - off;
  422. left -= ret;
  423. page_pos += didpages;
  424. pages_left -= didpages;
  425. /* hit stripe and need continue*/
  426. if (left && hit_stripe && pos < inode->i_size)
  427. goto more;
  428. }
  429. if (read > 0) {
  430. ret = read;
  431. /* did we bounce off eof? */
  432. if (pos + left > inode->i_size)
  433. *checkeof = CHECK_EOF;
  434. }
  435. dout("striped_read returns %d\n", ret);
  436. return ret;
  437. }
  438. /*
  439. * Completely synchronous read and write methods. Direct from __user
  440. * buffer to osd, or directly to user pages (if O_DIRECT).
  441. *
  442. * If the read spans object boundary, just do multiple reads.
  443. */
  444. static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *i,
  445. int *checkeof)
  446. {
  447. struct file *file = iocb->ki_filp;
  448. struct inode *inode = file_inode(file);
  449. struct page **pages;
  450. u64 off = iocb->ki_pos;
  451. int num_pages, ret;
  452. size_t len = iov_iter_count(i);
  453. dout("sync_read on file %p %llu~%u %s\n", file, off,
  454. (unsigned)len,
  455. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  456. if (!len)
  457. return 0;
  458. /*
  459. * flush any page cache pages in this range. this
  460. * will make concurrent normal and sync io slow,
  461. * but it will at least behave sensibly when they are
  462. * in sequence.
  463. */
  464. ret = filemap_write_and_wait_range(inode->i_mapping, off,
  465. off + len);
  466. if (ret < 0)
  467. return ret;
  468. if (iocb->ki_flags & IOCB_DIRECT) {
  469. while (iov_iter_count(i)) {
  470. size_t start;
  471. ssize_t n;
  472. n = dio_get_pagev_size(i);
  473. pages = dio_get_pages_alloc(i, n, &start, &num_pages);
  474. if (IS_ERR(pages))
  475. return PTR_ERR(pages);
  476. ret = striped_read(inode, off, n,
  477. pages, num_pages, checkeof,
  478. 1, start);
  479. ceph_put_page_vector(pages, num_pages, true);
  480. if (ret <= 0)
  481. break;
  482. off += ret;
  483. iov_iter_advance(i, ret);
  484. if (ret < n)
  485. break;
  486. }
  487. } else {
  488. num_pages = calc_pages_for(off, len);
  489. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  490. if (IS_ERR(pages))
  491. return PTR_ERR(pages);
  492. ret = striped_read(inode, off, len, pages,
  493. num_pages, checkeof, 0, 0);
  494. if (ret > 0) {
  495. int l, k = 0;
  496. size_t left = ret;
  497. while (left) {
  498. size_t page_off = off & ~PAGE_MASK;
  499. size_t copy = min_t(size_t,
  500. PAGE_SIZE - page_off, left);
  501. l = copy_page_to_iter(pages[k++], page_off,
  502. copy, i);
  503. off += l;
  504. left -= l;
  505. if (l < copy)
  506. break;
  507. }
  508. }
  509. ceph_release_page_vector(pages, num_pages);
  510. }
  511. if (off > iocb->ki_pos) {
  512. ret = off - iocb->ki_pos;
  513. iocb->ki_pos = off;
  514. }
  515. dout("sync_read result %d\n", ret);
  516. return ret;
  517. }
  518. /*
  519. * Write commit request unsafe callback, called to tell us when a
  520. * request is unsafe (that is, in flight--has been handed to the
  521. * messenger to send to its target osd). It is called again when
  522. * we've received a response message indicating the request is
  523. * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
  524. * is completed early (and unsuccessfully) due to a timeout or
  525. * interrupt.
  526. *
  527. * This is used if we requested both an ACK and ONDISK commit reply
  528. * from the OSD.
  529. */
  530. static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
  531. {
  532. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  533. dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
  534. unsafe ? "un" : "");
  535. if (unsafe) {
  536. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  537. spin_lock(&ci->i_unsafe_lock);
  538. list_add_tail(&req->r_unsafe_item,
  539. &ci->i_unsafe_writes);
  540. spin_unlock(&ci->i_unsafe_lock);
  541. } else {
  542. spin_lock(&ci->i_unsafe_lock);
  543. list_del_init(&req->r_unsafe_item);
  544. spin_unlock(&ci->i_unsafe_lock);
  545. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  546. }
  547. }
  548. /*
  549. * Synchronous write, straight from __user pointer or user pages.
  550. *
  551. * If write spans object boundary, just do multiple writes. (For a
  552. * correct atomic write, we should e.g. take write locks on all
  553. * objects, rollback on failure, etc.)
  554. */
  555. static ssize_t
  556. ceph_sync_direct_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
  557. struct ceph_snap_context *snapc)
  558. {
  559. struct file *file = iocb->ki_filp;
  560. struct inode *inode = file_inode(file);
  561. struct ceph_inode_info *ci = ceph_inode(inode);
  562. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  563. struct ceph_vino vino;
  564. struct ceph_osd_request *req;
  565. struct page **pages;
  566. int num_pages;
  567. int written = 0;
  568. int flags;
  569. int check_caps = 0;
  570. int ret;
  571. struct timespec mtime = CURRENT_TIME;
  572. size_t count = iov_iter_count(from);
  573. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  574. return -EROFS;
  575. dout("sync_direct_write on file %p %lld~%u\n", file, pos,
  576. (unsigned)count);
  577. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  578. if (ret < 0)
  579. return ret;
  580. ret = invalidate_inode_pages2_range(inode->i_mapping,
  581. pos >> PAGE_CACHE_SHIFT,
  582. (pos + count) >> PAGE_CACHE_SHIFT);
  583. if (ret < 0)
  584. dout("invalidate_inode_pages2_range returned %d\n", ret);
  585. flags = CEPH_OSD_FLAG_ORDERSNAP |
  586. CEPH_OSD_FLAG_ONDISK |
  587. CEPH_OSD_FLAG_WRITE;
  588. while (iov_iter_count(from) > 0) {
  589. u64 len = dio_get_pagev_size(from);
  590. size_t start;
  591. ssize_t n;
  592. vino = ceph_vino(inode);
  593. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  594. vino, pos, &len, 0,
  595. 2,/*include a 'startsync' command*/
  596. CEPH_OSD_OP_WRITE, flags, snapc,
  597. ci->i_truncate_seq,
  598. ci->i_truncate_size,
  599. false);
  600. if (IS_ERR(req)) {
  601. ret = PTR_ERR(req);
  602. break;
  603. }
  604. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC, 0);
  605. n = len;
  606. pages = dio_get_pages_alloc(from, len, &start, &num_pages);
  607. if (IS_ERR(pages)) {
  608. ceph_osdc_put_request(req);
  609. ret = PTR_ERR(pages);
  610. break;
  611. }
  612. /*
  613. * throw out any page cache pages in this range. this
  614. * may block.
  615. */
  616. truncate_inode_pages_range(inode->i_mapping, pos,
  617. (pos+n) | (PAGE_CACHE_SIZE-1));
  618. osd_req_op_extent_osd_data_pages(req, 0, pages, n, start,
  619. false, false);
  620. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  621. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  622. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  623. if (!ret)
  624. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  625. ceph_put_page_vector(pages, num_pages, false);
  626. ceph_osdc_put_request(req);
  627. if (ret)
  628. break;
  629. pos += n;
  630. written += n;
  631. iov_iter_advance(from, n);
  632. if (pos > i_size_read(inode)) {
  633. check_caps = ceph_inode_set_size(inode, pos);
  634. if (check_caps)
  635. ceph_check_caps(ceph_inode(inode),
  636. CHECK_CAPS_AUTHONLY,
  637. NULL);
  638. }
  639. }
  640. if (ret != -EOLDSNAPC && written > 0) {
  641. iocb->ki_pos = pos;
  642. ret = written;
  643. }
  644. return ret;
  645. }
  646. /*
  647. * Synchronous write, straight from __user pointer or user pages.
  648. *
  649. * If write spans object boundary, just do multiple writes. (For a
  650. * correct atomic write, we should e.g. take write locks on all
  651. * objects, rollback on failure, etc.)
  652. */
  653. static ssize_t
  654. ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
  655. struct ceph_snap_context *snapc)
  656. {
  657. struct file *file = iocb->ki_filp;
  658. struct inode *inode = file_inode(file);
  659. struct ceph_inode_info *ci = ceph_inode(inode);
  660. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  661. struct ceph_vino vino;
  662. struct ceph_osd_request *req;
  663. struct page **pages;
  664. u64 len;
  665. int num_pages;
  666. int written = 0;
  667. int flags;
  668. int check_caps = 0;
  669. int ret;
  670. struct timespec mtime = CURRENT_TIME;
  671. size_t count = iov_iter_count(from);
  672. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  673. return -EROFS;
  674. dout("sync_write on file %p %lld~%u\n", file, pos, (unsigned)count);
  675. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  676. if (ret < 0)
  677. return ret;
  678. ret = invalidate_inode_pages2_range(inode->i_mapping,
  679. pos >> PAGE_CACHE_SHIFT,
  680. (pos + count) >> PAGE_CACHE_SHIFT);
  681. if (ret < 0)
  682. dout("invalidate_inode_pages2_range returned %d\n", ret);
  683. flags = CEPH_OSD_FLAG_ORDERSNAP |
  684. CEPH_OSD_FLAG_ONDISK |
  685. CEPH_OSD_FLAG_WRITE |
  686. CEPH_OSD_FLAG_ACK;
  687. while ((len = iov_iter_count(from)) > 0) {
  688. size_t left;
  689. int n;
  690. vino = ceph_vino(inode);
  691. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  692. vino, pos, &len, 0, 1,
  693. CEPH_OSD_OP_WRITE, flags, snapc,
  694. ci->i_truncate_seq,
  695. ci->i_truncate_size,
  696. false);
  697. if (IS_ERR(req)) {
  698. ret = PTR_ERR(req);
  699. break;
  700. }
  701. /*
  702. * write from beginning of first page,
  703. * regardless of io alignment
  704. */
  705. num_pages = (len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  706. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  707. if (IS_ERR(pages)) {
  708. ret = PTR_ERR(pages);
  709. goto out;
  710. }
  711. left = len;
  712. for (n = 0; n < num_pages; n++) {
  713. size_t plen = min_t(size_t, left, PAGE_SIZE);
  714. ret = copy_page_from_iter(pages[n], 0, plen, from);
  715. if (ret != plen) {
  716. ret = -EFAULT;
  717. break;
  718. }
  719. left -= ret;
  720. }
  721. if (ret < 0) {
  722. ceph_release_page_vector(pages, num_pages);
  723. goto out;
  724. }
  725. /* get a second commit callback */
  726. req->r_unsafe_callback = ceph_sync_write_unsafe;
  727. req->r_inode = inode;
  728. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
  729. false, true);
  730. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  731. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  732. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  733. if (!ret)
  734. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  735. out:
  736. ceph_osdc_put_request(req);
  737. if (ret == 0) {
  738. pos += len;
  739. written += len;
  740. if (pos > i_size_read(inode)) {
  741. check_caps = ceph_inode_set_size(inode, pos);
  742. if (check_caps)
  743. ceph_check_caps(ceph_inode(inode),
  744. CHECK_CAPS_AUTHONLY,
  745. NULL);
  746. }
  747. } else
  748. break;
  749. }
  750. if (ret != -EOLDSNAPC && written > 0) {
  751. ret = written;
  752. iocb->ki_pos = pos;
  753. }
  754. return ret;
  755. }
  756. /*
  757. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  758. * Atomically grab references, so that those bits are not released
  759. * back to the MDS mid-read.
  760. *
  761. * Hmm, the sync read case isn't actually async... should it be?
  762. */
  763. static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
  764. {
  765. struct file *filp = iocb->ki_filp;
  766. struct ceph_file_info *fi = filp->private_data;
  767. size_t len = iov_iter_count(to);
  768. struct inode *inode = file_inode(filp);
  769. struct ceph_inode_info *ci = ceph_inode(inode);
  770. struct page *pinned_page = NULL;
  771. ssize_t ret;
  772. int want, got = 0;
  773. int retry_op = 0, read = 0;
  774. again:
  775. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  776. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
  777. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  778. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  779. else
  780. want = CEPH_CAP_FILE_CACHE;
  781. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
  782. if (ret < 0)
  783. return ret;
  784. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  785. (iocb->ki_flags & IOCB_DIRECT) ||
  786. (fi->flags & CEPH_F_SYNC)) {
  787. dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  788. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  789. ceph_cap_string(got));
  790. if (ci->i_inline_version == CEPH_INLINE_NONE) {
  791. /* hmm, this isn't really async... */
  792. ret = ceph_sync_read(iocb, to, &retry_op);
  793. } else {
  794. retry_op = READ_INLINE;
  795. }
  796. } else {
  797. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  798. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  799. ceph_cap_string(got));
  800. ret = generic_file_read_iter(iocb, to);
  801. }
  802. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  803. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  804. if (pinned_page) {
  805. page_cache_release(pinned_page);
  806. pinned_page = NULL;
  807. }
  808. ceph_put_cap_refs(ci, got);
  809. if (retry_op && ret >= 0) {
  810. int statret;
  811. struct page *page = NULL;
  812. loff_t i_size;
  813. if (retry_op == READ_INLINE) {
  814. page = __page_cache_alloc(GFP_KERNEL);
  815. if (!page)
  816. return -ENOMEM;
  817. }
  818. statret = __ceph_do_getattr(inode, page,
  819. CEPH_STAT_CAP_INLINE_DATA, !!page);
  820. if (statret < 0) {
  821. if (page)
  822. __free_page(page);
  823. if (statret == -ENODATA) {
  824. BUG_ON(retry_op != READ_INLINE);
  825. goto again;
  826. }
  827. return statret;
  828. }
  829. i_size = i_size_read(inode);
  830. if (retry_op == READ_INLINE) {
  831. BUG_ON(ret > 0 || read > 0);
  832. if (iocb->ki_pos < i_size &&
  833. iocb->ki_pos < PAGE_CACHE_SIZE) {
  834. loff_t end = min_t(loff_t, i_size,
  835. iocb->ki_pos + len);
  836. end = min_t(loff_t, end, PAGE_CACHE_SIZE);
  837. if (statret < end)
  838. zero_user_segment(page, statret, end);
  839. ret = copy_page_to_iter(page,
  840. iocb->ki_pos & ~PAGE_MASK,
  841. end - iocb->ki_pos, to);
  842. iocb->ki_pos += ret;
  843. read += ret;
  844. }
  845. if (iocb->ki_pos < i_size && read < len) {
  846. size_t zlen = min_t(size_t, len - read,
  847. i_size - iocb->ki_pos);
  848. ret = iov_iter_zero(zlen, to);
  849. iocb->ki_pos += ret;
  850. read += ret;
  851. }
  852. __free_pages(page, 0);
  853. return read;
  854. }
  855. /* hit EOF or hole? */
  856. if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
  857. ret < len) {
  858. dout("sync_read hit hole, ppos %lld < size %lld"
  859. ", reading more\n", iocb->ki_pos,
  860. inode->i_size);
  861. read += ret;
  862. len -= ret;
  863. retry_op = 0;
  864. goto again;
  865. }
  866. }
  867. if (ret >= 0)
  868. ret += read;
  869. return ret;
  870. }
  871. /*
  872. * Take cap references to avoid releasing caps to MDS mid-write.
  873. *
  874. * If we are synchronous, and write with an old snap context, the OSD
  875. * may return EOLDSNAPC. In that case, retry the write.. _after_
  876. * dropping our cap refs and allowing the pending snap to logically
  877. * complete _before_ this write occurs.
  878. *
  879. * If we are near ENOSPC, write synchronously.
  880. */
  881. static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
  882. {
  883. struct file *file = iocb->ki_filp;
  884. struct ceph_file_info *fi = file->private_data;
  885. struct inode *inode = file_inode(file);
  886. struct ceph_inode_info *ci = ceph_inode(inode);
  887. struct ceph_osd_client *osdc =
  888. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  889. struct ceph_cap_flush *prealloc_cf;
  890. ssize_t count, written = 0;
  891. int err, want, got;
  892. loff_t pos;
  893. if (ceph_snap(inode) != CEPH_NOSNAP)
  894. return -EROFS;
  895. prealloc_cf = ceph_alloc_cap_flush();
  896. if (!prealloc_cf)
  897. return -ENOMEM;
  898. mutex_lock(&inode->i_mutex);
  899. /* We can write back this queue in page reclaim */
  900. current->backing_dev_info = inode_to_bdi(inode);
  901. if (iocb->ki_flags & IOCB_APPEND) {
  902. err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  903. if (err < 0)
  904. goto out;
  905. }
  906. err = generic_write_checks(iocb, from);
  907. if (err <= 0)
  908. goto out;
  909. pos = iocb->ki_pos;
  910. count = iov_iter_count(from);
  911. err = file_remove_privs(file);
  912. if (err)
  913. goto out;
  914. err = file_update_time(file);
  915. if (err)
  916. goto out;
  917. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  918. err = ceph_uninline_data(file, NULL);
  919. if (err < 0)
  920. goto out;
  921. }
  922. retry_snap:
  923. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) {
  924. err = -ENOSPC;
  925. goto out;
  926. }
  927. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  928. inode, ceph_vinop(inode), pos, count, inode->i_size);
  929. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  930. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  931. else
  932. want = CEPH_CAP_FILE_BUFFER;
  933. got = 0;
  934. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, pos + count,
  935. &got, NULL);
  936. if (err < 0)
  937. goto out;
  938. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  939. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  940. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  941. (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC)) {
  942. struct ceph_snap_context *snapc;
  943. struct iov_iter data;
  944. mutex_unlock(&inode->i_mutex);
  945. spin_lock(&ci->i_ceph_lock);
  946. if (__ceph_have_pending_cap_snap(ci)) {
  947. struct ceph_cap_snap *capsnap =
  948. list_last_entry(&ci->i_cap_snaps,
  949. struct ceph_cap_snap,
  950. ci_item);
  951. snapc = ceph_get_snap_context(capsnap->context);
  952. } else {
  953. BUG_ON(!ci->i_head_snapc);
  954. snapc = ceph_get_snap_context(ci->i_head_snapc);
  955. }
  956. spin_unlock(&ci->i_ceph_lock);
  957. /* we might need to revert back to that point */
  958. data = *from;
  959. if (iocb->ki_flags & IOCB_DIRECT)
  960. written = ceph_sync_direct_write(iocb, &data, pos,
  961. snapc);
  962. else
  963. written = ceph_sync_write(iocb, &data, pos, snapc);
  964. if (written == -EOLDSNAPC) {
  965. dout("aio_write %p %llx.%llx %llu~%u"
  966. "got EOLDSNAPC, retrying\n",
  967. inode, ceph_vinop(inode),
  968. pos, (unsigned)count);
  969. mutex_lock(&inode->i_mutex);
  970. goto retry_snap;
  971. }
  972. if (written > 0)
  973. iov_iter_advance(from, written);
  974. ceph_put_snap_context(snapc);
  975. } else {
  976. loff_t old_size = inode->i_size;
  977. /*
  978. * No need to acquire the i_truncate_mutex. Because
  979. * the MDS revokes Fwb caps before sending truncate
  980. * message to us. We can't get Fwb cap while there
  981. * are pending vmtruncate. So write and vmtruncate
  982. * can not run at the same time
  983. */
  984. written = generic_perform_write(file, from, pos);
  985. if (likely(written >= 0))
  986. iocb->ki_pos = pos + written;
  987. if (inode->i_size > old_size)
  988. ceph_fscache_update_objectsize(inode);
  989. mutex_unlock(&inode->i_mutex);
  990. }
  991. if (written >= 0) {
  992. int dirty;
  993. spin_lock(&ci->i_ceph_lock);
  994. ci->i_inline_version = CEPH_INLINE_NONE;
  995. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  996. &prealloc_cf);
  997. spin_unlock(&ci->i_ceph_lock);
  998. if (dirty)
  999. __mark_inode_dirty(inode, dirty);
  1000. }
  1001. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  1002. inode, ceph_vinop(inode), pos, (unsigned)count,
  1003. ceph_cap_string(got));
  1004. ceph_put_cap_refs(ci, got);
  1005. if (written >= 0 &&
  1006. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) ||
  1007. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
  1008. err = vfs_fsync_range(file, pos, pos + written - 1, 1);
  1009. if (err < 0)
  1010. written = err;
  1011. }
  1012. goto out_unlocked;
  1013. out:
  1014. mutex_unlock(&inode->i_mutex);
  1015. out_unlocked:
  1016. ceph_free_cap_flush(prealloc_cf);
  1017. current->backing_dev_info = NULL;
  1018. return written ? written : err;
  1019. }
  1020. /*
  1021. * llseek. be sure to verify file size on SEEK_END.
  1022. */
  1023. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  1024. {
  1025. struct inode *inode = file->f_mapping->host;
  1026. int ret;
  1027. mutex_lock(&inode->i_mutex);
  1028. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  1029. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1030. if (ret < 0) {
  1031. offset = ret;
  1032. goto out;
  1033. }
  1034. }
  1035. switch (whence) {
  1036. case SEEK_END:
  1037. offset += inode->i_size;
  1038. break;
  1039. case SEEK_CUR:
  1040. /*
  1041. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  1042. * position-querying operation. Avoid rewriting the "same"
  1043. * f_pos value back to the file because a concurrent read(),
  1044. * write() or lseek() might have altered it
  1045. */
  1046. if (offset == 0) {
  1047. offset = file->f_pos;
  1048. goto out;
  1049. }
  1050. offset += file->f_pos;
  1051. break;
  1052. case SEEK_DATA:
  1053. if (offset >= inode->i_size) {
  1054. ret = -ENXIO;
  1055. goto out;
  1056. }
  1057. break;
  1058. case SEEK_HOLE:
  1059. if (offset >= inode->i_size) {
  1060. ret = -ENXIO;
  1061. goto out;
  1062. }
  1063. offset = inode->i_size;
  1064. break;
  1065. }
  1066. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  1067. out:
  1068. mutex_unlock(&inode->i_mutex);
  1069. return offset;
  1070. }
  1071. static inline void ceph_zero_partial_page(
  1072. struct inode *inode, loff_t offset, unsigned size)
  1073. {
  1074. struct page *page;
  1075. pgoff_t index = offset >> PAGE_CACHE_SHIFT;
  1076. page = find_lock_page(inode->i_mapping, index);
  1077. if (page) {
  1078. wait_on_page_writeback(page);
  1079. zero_user(page, offset & (PAGE_CACHE_SIZE - 1), size);
  1080. unlock_page(page);
  1081. page_cache_release(page);
  1082. }
  1083. }
  1084. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  1085. loff_t length)
  1086. {
  1087. loff_t nearly = round_up(offset, PAGE_CACHE_SIZE);
  1088. if (offset < nearly) {
  1089. loff_t size = nearly - offset;
  1090. if (length < size)
  1091. size = length;
  1092. ceph_zero_partial_page(inode, offset, size);
  1093. offset += size;
  1094. length -= size;
  1095. }
  1096. if (length >= PAGE_CACHE_SIZE) {
  1097. loff_t size = round_down(length, PAGE_CACHE_SIZE);
  1098. truncate_pagecache_range(inode, offset, offset + size - 1);
  1099. offset += size;
  1100. length -= size;
  1101. }
  1102. if (length)
  1103. ceph_zero_partial_page(inode, offset, length);
  1104. }
  1105. static int ceph_zero_partial_object(struct inode *inode,
  1106. loff_t offset, loff_t *length)
  1107. {
  1108. struct ceph_inode_info *ci = ceph_inode(inode);
  1109. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1110. struct ceph_osd_request *req;
  1111. int ret = 0;
  1112. loff_t zero = 0;
  1113. int op;
  1114. if (!length) {
  1115. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  1116. length = &zero;
  1117. } else {
  1118. op = CEPH_OSD_OP_ZERO;
  1119. }
  1120. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1121. ceph_vino(inode),
  1122. offset, length,
  1123. 0, 1, op,
  1124. CEPH_OSD_FLAG_WRITE |
  1125. CEPH_OSD_FLAG_ONDISK,
  1126. NULL, 0, 0, false);
  1127. if (IS_ERR(req)) {
  1128. ret = PTR_ERR(req);
  1129. goto out;
  1130. }
  1131. ceph_osdc_build_request(req, offset, NULL, ceph_vino(inode).snap,
  1132. &inode->i_mtime);
  1133. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1134. if (!ret) {
  1135. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1136. if (ret == -ENOENT)
  1137. ret = 0;
  1138. }
  1139. ceph_osdc_put_request(req);
  1140. out:
  1141. return ret;
  1142. }
  1143. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  1144. {
  1145. int ret = 0;
  1146. struct ceph_inode_info *ci = ceph_inode(inode);
  1147. s32 stripe_unit = ceph_file_layout_su(ci->i_layout);
  1148. s32 stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
  1149. s32 object_size = ceph_file_layout_object_size(ci->i_layout);
  1150. u64 object_set_size = object_size * stripe_count;
  1151. u64 nearly, t;
  1152. /* round offset up to next period boundary */
  1153. nearly = offset + object_set_size - 1;
  1154. t = nearly;
  1155. nearly -= do_div(t, object_set_size);
  1156. while (length && offset < nearly) {
  1157. loff_t size = length;
  1158. ret = ceph_zero_partial_object(inode, offset, &size);
  1159. if (ret < 0)
  1160. return ret;
  1161. offset += size;
  1162. length -= size;
  1163. }
  1164. while (length >= object_set_size) {
  1165. int i;
  1166. loff_t pos = offset;
  1167. for (i = 0; i < stripe_count; ++i) {
  1168. ret = ceph_zero_partial_object(inode, pos, NULL);
  1169. if (ret < 0)
  1170. return ret;
  1171. pos += stripe_unit;
  1172. }
  1173. offset += object_set_size;
  1174. length -= object_set_size;
  1175. }
  1176. while (length) {
  1177. loff_t size = length;
  1178. ret = ceph_zero_partial_object(inode, offset, &size);
  1179. if (ret < 0)
  1180. return ret;
  1181. offset += size;
  1182. length -= size;
  1183. }
  1184. return ret;
  1185. }
  1186. static long ceph_fallocate(struct file *file, int mode,
  1187. loff_t offset, loff_t length)
  1188. {
  1189. struct ceph_file_info *fi = file->private_data;
  1190. struct inode *inode = file_inode(file);
  1191. struct ceph_inode_info *ci = ceph_inode(inode);
  1192. struct ceph_osd_client *osdc =
  1193. &ceph_inode_to_client(inode)->client->osdc;
  1194. struct ceph_cap_flush *prealloc_cf;
  1195. int want, got = 0;
  1196. int dirty;
  1197. int ret = 0;
  1198. loff_t endoff = 0;
  1199. loff_t size;
  1200. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1201. return -EOPNOTSUPP;
  1202. if (!S_ISREG(inode->i_mode))
  1203. return -EOPNOTSUPP;
  1204. prealloc_cf = ceph_alloc_cap_flush();
  1205. if (!prealloc_cf)
  1206. return -ENOMEM;
  1207. mutex_lock(&inode->i_mutex);
  1208. if (ceph_snap(inode) != CEPH_NOSNAP) {
  1209. ret = -EROFS;
  1210. goto unlock;
  1211. }
  1212. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) &&
  1213. !(mode & FALLOC_FL_PUNCH_HOLE)) {
  1214. ret = -ENOSPC;
  1215. goto unlock;
  1216. }
  1217. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1218. ret = ceph_uninline_data(file, NULL);
  1219. if (ret < 0)
  1220. goto unlock;
  1221. }
  1222. size = i_size_read(inode);
  1223. if (!(mode & FALLOC_FL_KEEP_SIZE))
  1224. endoff = offset + length;
  1225. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1226. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1227. else
  1228. want = CEPH_CAP_FILE_BUFFER;
  1229. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, endoff, &got, NULL);
  1230. if (ret < 0)
  1231. goto unlock;
  1232. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1233. if (offset < size)
  1234. ceph_zero_pagecache_range(inode, offset, length);
  1235. ret = ceph_zero_objects(inode, offset, length);
  1236. } else if (endoff > size) {
  1237. truncate_pagecache_range(inode, size, -1);
  1238. if (ceph_inode_set_size(inode, endoff))
  1239. ceph_check_caps(ceph_inode(inode),
  1240. CHECK_CAPS_AUTHONLY, NULL);
  1241. }
  1242. if (!ret) {
  1243. spin_lock(&ci->i_ceph_lock);
  1244. ci->i_inline_version = CEPH_INLINE_NONE;
  1245. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1246. &prealloc_cf);
  1247. spin_unlock(&ci->i_ceph_lock);
  1248. if (dirty)
  1249. __mark_inode_dirty(inode, dirty);
  1250. }
  1251. ceph_put_cap_refs(ci, got);
  1252. unlock:
  1253. mutex_unlock(&inode->i_mutex);
  1254. ceph_free_cap_flush(prealloc_cf);
  1255. return ret;
  1256. }
  1257. const struct file_operations ceph_file_fops = {
  1258. .open = ceph_open,
  1259. .release = ceph_release,
  1260. .llseek = ceph_llseek,
  1261. .read_iter = ceph_read_iter,
  1262. .write_iter = ceph_write_iter,
  1263. .mmap = ceph_mmap,
  1264. .fsync = ceph_fsync,
  1265. .lock = ceph_lock,
  1266. .flock = ceph_flock,
  1267. .splice_read = generic_file_splice_read,
  1268. .splice_write = iter_file_splice_write,
  1269. .unlocked_ioctl = ceph_ioctl,
  1270. .compat_ioctl = ceph_ioctl,
  1271. .fallocate = ceph_fallocate,
  1272. };