file.c 35 KB

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