file.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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/aio.h>
  10. #include <linux/falloc.h>
  11. #include "super.h"
  12. #include "mds_client.h"
  13. #include "cache.h"
  14. /*
  15. * Ceph file operations
  16. *
  17. * Implement basic open/close functionality, and implement
  18. * read/write.
  19. *
  20. * We implement three modes of file I/O:
  21. * - buffered uses the generic_file_aio_{read,write} helpers
  22. *
  23. * - synchronous is used when there is multi-client read/write
  24. * sharing, avoids the page cache, and synchronously waits for an
  25. * ack from the OSD.
  26. *
  27. * - direct io takes the variant of the sync path that references
  28. * user pages directly.
  29. *
  30. * fsync() flushes and waits on dirty pages, but just queues metadata
  31. * for writeback: since the MDS can recover size and mtime there is no
  32. * need to wait for MDS acknowledgement.
  33. */
  34. /*
  35. * Prepare an open request. Preallocate ceph_cap to avoid an
  36. * inopportune ENOMEM later.
  37. */
  38. static struct ceph_mds_request *
  39. prepare_open_request(struct super_block *sb, int flags, int create_mode)
  40. {
  41. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  42. struct ceph_mds_client *mdsc = fsc->mdsc;
  43. struct ceph_mds_request *req;
  44. int want_auth = USE_ANY_MDS;
  45. int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
  46. if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
  47. want_auth = USE_AUTH_MDS;
  48. req = ceph_mdsc_create_request(mdsc, op, want_auth);
  49. if (IS_ERR(req))
  50. goto out;
  51. req->r_fmode = ceph_flags_to_mode(flags);
  52. req->r_args.open.flags = cpu_to_le32(flags);
  53. req->r_args.open.mode = cpu_to_le32(create_mode);
  54. out:
  55. return req;
  56. }
  57. /*
  58. * initialize private struct file data.
  59. * if we fail, clean up by dropping fmode reference on the ceph_inode
  60. */
  61. static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
  62. {
  63. struct ceph_file_info *cf;
  64. int ret = 0;
  65. struct ceph_inode_info *ci = ceph_inode(inode);
  66. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  67. struct ceph_mds_client *mdsc = fsc->mdsc;
  68. switch (inode->i_mode & S_IFMT) {
  69. case S_IFREG:
  70. /* First file open request creates the cookie, we want to keep
  71. * this cookie around for the filetime of the inode as not to
  72. * have to worry about fscache register / revoke / operation
  73. * races.
  74. *
  75. * Also, if we know the operation is going to invalidate data
  76. * (non readonly) just nuke the cache right away.
  77. */
  78. ceph_fscache_register_inode_cookie(mdsc->fsc, ci);
  79. if ((fmode & CEPH_FILE_MODE_WR))
  80. ceph_fscache_invalidate(inode);
  81. case S_IFDIR:
  82. dout("init_file %p %p 0%o (regular)\n", inode, file,
  83. inode->i_mode);
  84. cf = kmem_cache_alloc(ceph_file_cachep, GFP_NOFS | __GFP_ZERO);
  85. if (cf == NULL) {
  86. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  87. return -ENOMEM;
  88. }
  89. cf->fmode = fmode;
  90. cf->next_offset = 2;
  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_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. int err;
  212. dout("atomic_open %p dentry %p '%.*s' %s flags %d mode 0%o\n",
  213. dir, dentry, dentry->d_name.len, dentry->d_name.name,
  214. d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
  215. if (dentry->d_name.len > NAME_MAX)
  216. return -ENAMETOOLONG;
  217. err = ceph_init_dentry(dentry);
  218. if (err < 0)
  219. return err;
  220. /* do the open */
  221. req = prepare_open_request(dir->i_sb, flags, mode);
  222. if (IS_ERR(req))
  223. return PTR_ERR(req);
  224. req->r_dentry = dget(dentry);
  225. req->r_num_caps = 2;
  226. if (flags & O_CREAT) {
  227. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  228. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  229. }
  230. req->r_locked_dir = dir; /* caller holds dir->i_mutex */
  231. err = ceph_mdsc_do_request(mdsc,
  232. (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
  233. req);
  234. if (err)
  235. goto out_err;
  236. err = ceph_handle_snapdir(req, dentry, err);
  237. if (err == 0 && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  238. err = ceph_handle_notrace_create(dir, dentry);
  239. if (d_unhashed(dentry)) {
  240. dn = ceph_finish_lookup(req, dentry, err);
  241. if (IS_ERR(dn))
  242. err = PTR_ERR(dn);
  243. } else {
  244. /* we were given a hashed negative dentry */
  245. dn = NULL;
  246. }
  247. if (err)
  248. goto out_err;
  249. if (dn || dentry->d_inode == NULL || S_ISLNK(dentry->d_inode->i_mode)) {
  250. /* make vfs retry on splice, ENOENT, or symlink */
  251. dout("atomic_open finish_no_open on dn %p\n", dn);
  252. err = finish_no_open(file, dn);
  253. } else {
  254. dout("atomic_open finish_open on dn %p\n", dn);
  255. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  256. ceph_init_acl(dentry, dentry->d_inode, dir);
  257. *opened |= FILE_CREATED;
  258. }
  259. err = finish_open(file, dentry, ceph_open, opened);
  260. }
  261. out_err:
  262. if (!req->r_err && req->r_target_inode)
  263. ceph_put_fmode(ceph_inode(req->r_target_inode), req->r_fmode);
  264. ceph_mdsc_put_request(req);
  265. dout("atomic_open result=%d\n", err);
  266. return err;
  267. }
  268. int ceph_release(struct inode *inode, struct file *file)
  269. {
  270. struct ceph_inode_info *ci = ceph_inode(inode);
  271. struct ceph_file_info *cf = file->private_data;
  272. dout("release inode %p file %p\n", inode, file);
  273. ceph_put_fmode(ci, cf->fmode);
  274. if (cf->last_readdir)
  275. ceph_mdsc_put_request(cf->last_readdir);
  276. kfree(cf->last_name);
  277. kfree(cf->dir_info);
  278. dput(cf->dentry);
  279. kmem_cache_free(ceph_file_cachep, cf);
  280. /* wake up anyone waiting for caps on this inode */
  281. wake_up_all(&ci->i_cap_wq);
  282. return 0;
  283. }
  284. /*
  285. * Read a range of bytes striped over one or more objects. Iterate over
  286. * objects we stripe over. (That's not atomic, but good enough for now.)
  287. *
  288. * If we get a short result from the OSD, check against i_size; we need to
  289. * only return a short read to the caller if we hit EOF.
  290. */
  291. static int striped_read(struct inode *inode,
  292. u64 off, u64 len,
  293. struct page **pages, int num_pages,
  294. int *checkeof, bool o_direct,
  295. unsigned long buf_align)
  296. {
  297. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  298. struct ceph_inode_info *ci = ceph_inode(inode);
  299. u64 pos, this_len, left;
  300. int io_align, page_align;
  301. int pages_left;
  302. int read;
  303. struct page **page_pos;
  304. int ret;
  305. bool hit_stripe, was_short;
  306. /*
  307. * we may need to do multiple reads. not atomic, unfortunately.
  308. */
  309. pos = off;
  310. left = len;
  311. page_pos = pages;
  312. pages_left = num_pages;
  313. read = 0;
  314. io_align = off & ~PAGE_MASK;
  315. more:
  316. if (o_direct)
  317. page_align = (pos - io_align + buf_align) & ~PAGE_MASK;
  318. else
  319. page_align = pos & ~PAGE_MASK;
  320. this_len = left;
  321. ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
  322. &ci->i_layout, pos, &this_len,
  323. ci->i_truncate_seq,
  324. ci->i_truncate_size,
  325. page_pos, pages_left, page_align);
  326. if (ret == -ENOENT)
  327. ret = 0;
  328. hit_stripe = this_len < left;
  329. was_short = ret >= 0 && ret < this_len;
  330. dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, left, read,
  331. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  332. if (ret >= 0) {
  333. int didpages;
  334. if (was_short && (pos + ret < inode->i_size)) {
  335. u64 tmp = min(this_len - ret,
  336. inode->i_size - pos - ret);
  337. dout(" zero gap %llu to %llu\n",
  338. pos + ret, pos + ret + tmp);
  339. ceph_zero_page_vector_range(page_align + read + ret,
  340. tmp, pages);
  341. ret += tmp;
  342. }
  343. didpages = (page_align + ret) >> PAGE_CACHE_SHIFT;
  344. pos += ret;
  345. read = pos - off;
  346. left -= ret;
  347. page_pos += didpages;
  348. pages_left -= didpages;
  349. /* hit stripe and need continue*/
  350. if (left && hit_stripe && pos < inode->i_size)
  351. goto more;
  352. }
  353. if (read > 0) {
  354. ret = read;
  355. /* did we bounce off eof? */
  356. if (pos + left > inode->i_size)
  357. *checkeof = 1;
  358. }
  359. dout("striped_read returns %d\n", ret);
  360. return ret;
  361. }
  362. /*
  363. * Completely synchronous read and write methods. Direct from __user
  364. * buffer to osd, or directly to user pages (if O_DIRECT).
  365. *
  366. * If the read spans object boundary, just do multiple reads.
  367. */
  368. static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *i,
  369. int *checkeof)
  370. {
  371. struct file *file = iocb->ki_filp;
  372. struct inode *inode = file_inode(file);
  373. struct page **pages;
  374. u64 off = iocb->ki_pos;
  375. int num_pages, ret;
  376. size_t len = i->count;
  377. dout("sync_read on file %p %llu~%u %s\n", file, off,
  378. (unsigned)len,
  379. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  380. /*
  381. * flush any page cache pages in this range. this
  382. * will make concurrent normal and sync io slow,
  383. * but it will at least behave sensibly when they are
  384. * in sequence.
  385. */
  386. ret = filemap_write_and_wait_range(inode->i_mapping, off,
  387. off + len);
  388. if (ret < 0)
  389. return ret;
  390. if (file->f_flags & O_DIRECT) {
  391. while (iov_iter_count(i)) {
  392. void __user *data = i->iov[0].iov_base + i->iov_offset;
  393. size_t len = i->iov[0].iov_len - i->iov_offset;
  394. num_pages = calc_pages_for((unsigned long)data, len);
  395. pages = ceph_get_direct_page_vector(data,
  396. num_pages, true);
  397. if (IS_ERR(pages))
  398. return PTR_ERR(pages);
  399. ret = striped_read(inode, off, len,
  400. pages, num_pages, checkeof,
  401. 1, (unsigned long)data & ~PAGE_MASK);
  402. ceph_put_page_vector(pages, num_pages, true);
  403. if (ret <= 0)
  404. break;
  405. off += ret;
  406. iov_iter_advance(i, ret);
  407. if (ret < len)
  408. break;
  409. }
  410. } else {
  411. num_pages = calc_pages_for(off, len);
  412. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  413. if (IS_ERR(pages))
  414. return PTR_ERR(pages);
  415. ret = striped_read(inode, off, len, pages,
  416. num_pages, checkeof, 0, 0);
  417. if (ret > 0) {
  418. int l, k = 0;
  419. size_t left = len = ret;
  420. while (left) {
  421. void __user *data = i->iov[0].iov_base
  422. + i->iov_offset;
  423. l = min(i->iov[0].iov_len - i->iov_offset,
  424. left);
  425. ret = ceph_copy_page_vector_to_user(&pages[k],
  426. data, off,
  427. l);
  428. if (ret > 0) {
  429. iov_iter_advance(i, ret);
  430. left -= ret;
  431. off += ret;
  432. k = calc_pages_for(iocb->ki_pos,
  433. len - left + 1) - 1;
  434. BUG_ON(k >= num_pages && left);
  435. } else
  436. break;
  437. }
  438. }
  439. ceph_release_page_vector(pages, num_pages);
  440. }
  441. if (off > iocb->ki_pos) {
  442. ret = off - iocb->ki_pos;
  443. iocb->ki_pos = off;
  444. }
  445. dout("sync_read result %d\n", ret);
  446. return ret;
  447. }
  448. /*
  449. * Write commit request unsafe callback, called to tell us when a
  450. * request is unsafe (that is, in flight--has been handed to the
  451. * messenger to send to its target osd). It is called again when
  452. * we've received a response message indicating the request is
  453. * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
  454. * is completed early (and unsuccessfully) due to a timeout or
  455. * interrupt.
  456. *
  457. * This is used if we requested both an ACK and ONDISK commit reply
  458. * from the OSD.
  459. */
  460. static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
  461. {
  462. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  463. dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
  464. unsafe ? "un" : "");
  465. if (unsafe) {
  466. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  467. spin_lock(&ci->i_unsafe_lock);
  468. list_add_tail(&req->r_unsafe_item,
  469. &ci->i_unsafe_writes);
  470. spin_unlock(&ci->i_unsafe_lock);
  471. } else {
  472. spin_lock(&ci->i_unsafe_lock);
  473. list_del_init(&req->r_unsafe_item);
  474. spin_unlock(&ci->i_unsafe_lock);
  475. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  476. }
  477. }
  478. /*
  479. * Synchronous write, straight from __user pointer or user pages.
  480. *
  481. * If write spans object boundary, just do multiple writes. (For a
  482. * correct atomic write, we should e.g. take write locks on all
  483. * objects, rollback on failure, etc.)
  484. */
  485. static ssize_t
  486. ceph_sync_direct_write(struct kiocb *iocb, const struct iovec *iov,
  487. unsigned long nr_segs, size_t count)
  488. {
  489. struct file *file = iocb->ki_filp;
  490. struct inode *inode = file_inode(file);
  491. struct ceph_inode_info *ci = ceph_inode(inode);
  492. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  493. struct ceph_snap_context *snapc;
  494. struct ceph_vino vino;
  495. struct ceph_osd_request *req;
  496. struct page **pages;
  497. int num_pages;
  498. int written = 0;
  499. int flags;
  500. int check_caps = 0;
  501. int page_align;
  502. int ret;
  503. struct timespec mtime = CURRENT_TIME;
  504. loff_t pos = iocb->ki_pos;
  505. struct iov_iter i;
  506. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  507. return -EROFS;
  508. dout("sync_direct_write on file %p %lld~%u\n", file, pos,
  509. (unsigned)count);
  510. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  511. if (ret < 0)
  512. return ret;
  513. ret = invalidate_inode_pages2_range(inode->i_mapping,
  514. pos >> PAGE_CACHE_SHIFT,
  515. (pos + count) >> PAGE_CACHE_SHIFT);
  516. if (ret < 0)
  517. dout("invalidate_inode_pages2_range returned %d\n", ret);
  518. flags = CEPH_OSD_FLAG_ORDERSNAP |
  519. CEPH_OSD_FLAG_ONDISK |
  520. CEPH_OSD_FLAG_WRITE;
  521. iov_iter_init(&i, iov, nr_segs, count, 0);
  522. while (iov_iter_count(&i) > 0) {
  523. void __user *data = i.iov->iov_base + i.iov_offset;
  524. u64 len = i.iov->iov_len - i.iov_offset;
  525. page_align = (unsigned long)data & ~PAGE_MASK;
  526. snapc = ci->i_snap_realm->cached_context;
  527. vino = ceph_vino(inode);
  528. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  529. vino, pos, &len,
  530. 2,/*include a 'startsync' command*/
  531. CEPH_OSD_OP_WRITE, flags, snapc,
  532. ci->i_truncate_seq,
  533. ci->i_truncate_size,
  534. false);
  535. if (IS_ERR(req)) {
  536. ret = PTR_ERR(req);
  537. break;
  538. }
  539. num_pages = calc_pages_for(page_align, len);
  540. pages = ceph_get_direct_page_vector(data, num_pages, false);
  541. if (IS_ERR(pages)) {
  542. ret = PTR_ERR(pages);
  543. goto out;
  544. }
  545. /*
  546. * throw out any page cache pages in this range. this
  547. * may block.
  548. */
  549. truncate_inode_pages_range(inode->i_mapping, pos,
  550. (pos+len) | (PAGE_CACHE_SIZE-1));
  551. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
  552. false, false);
  553. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  554. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  555. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  556. if (!ret)
  557. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  558. ceph_put_page_vector(pages, num_pages, false);
  559. out:
  560. ceph_osdc_put_request(req);
  561. if (ret == 0) {
  562. pos += len;
  563. written += len;
  564. iov_iter_advance(&i, (size_t)len);
  565. if (pos > i_size_read(inode)) {
  566. check_caps = ceph_inode_set_size(inode, pos);
  567. if (check_caps)
  568. ceph_check_caps(ceph_inode(inode),
  569. CHECK_CAPS_AUTHONLY,
  570. NULL);
  571. }
  572. } else
  573. break;
  574. }
  575. if (ret != -EOLDSNAPC && written > 0) {
  576. iocb->ki_pos = pos;
  577. ret = written;
  578. }
  579. return ret;
  580. }
  581. /*
  582. * Synchronous write, straight from __user pointer or user pages.
  583. *
  584. * If write spans object boundary, just do multiple writes. (For a
  585. * correct atomic write, we should e.g. take write locks on all
  586. * objects, rollback on failure, etc.)
  587. */
  588. static ssize_t ceph_sync_write(struct kiocb *iocb, const struct iovec *iov,
  589. unsigned long nr_segs, size_t count)
  590. {
  591. struct file *file = iocb->ki_filp;
  592. struct inode *inode = file_inode(file);
  593. struct ceph_inode_info *ci = ceph_inode(inode);
  594. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  595. struct ceph_snap_context *snapc;
  596. struct ceph_vino vino;
  597. struct ceph_osd_request *req;
  598. struct page **pages;
  599. u64 len;
  600. int num_pages;
  601. int written = 0;
  602. int flags;
  603. int check_caps = 0;
  604. int ret;
  605. struct timespec mtime = CURRENT_TIME;
  606. loff_t pos = iocb->ki_pos;
  607. struct iov_iter i;
  608. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  609. return -EROFS;
  610. dout("sync_write on file %p %lld~%u\n", file, pos, (unsigned)count);
  611. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  612. if (ret < 0)
  613. return ret;
  614. ret = invalidate_inode_pages2_range(inode->i_mapping,
  615. pos >> PAGE_CACHE_SHIFT,
  616. (pos + count) >> PAGE_CACHE_SHIFT);
  617. if (ret < 0)
  618. dout("invalidate_inode_pages2_range returned %d\n", ret);
  619. flags = CEPH_OSD_FLAG_ORDERSNAP |
  620. CEPH_OSD_FLAG_ONDISK |
  621. CEPH_OSD_FLAG_WRITE |
  622. CEPH_OSD_FLAG_ACK;
  623. iov_iter_init(&i, iov, nr_segs, count, 0);
  624. while ((len = iov_iter_count(&i)) > 0) {
  625. size_t left;
  626. int n;
  627. snapc = ci->i_snap_realm->cached_context;
  628. vino = ceph_vino(inode);
  629. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  630. vino, pos, &len, 1,
  631. CEPH_OSD_OP_WRITE, flags, snapc,
  632. ci->i_truncate_seq,
  633. ci->i_truncate_size,
  634. false);
  635. if (IS_ERR(req)) {
  636. ret = PTR_ERR(req);
  637. break;
  638. }
  639. /*
  640. * write from beginning of first page,
  641. * regardless of io alignment
  642. */
  643. num_pages = (len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  644. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  645. if (IS_ERR(pages)) {
  646. ret = PTR_ERR(pages);
  647. goto out;
  648. }
  649. left = len;
  650. for (n = 0; n < num_pages; n++) {
  651. size_t plen = min_t(size_t, left, PAGE_SIZE);
  652. ret = iov_iter_copy_from_user(pages[n], &i, 0, plen);
  653. if (ret != plen) {
  654. ret = -EFAULT;
  655. break;
  656. }
  657. left -= ret;
  658. iov_iter_advance(&i, ret);
  659. }
  660. if (ret < 0) {
  661. ceph_release_page_vector(pages, num_pages);
  662. goto out;
  663. }
  664. /* get a second commit callback */
  665. req->r_unsafe_callback = ceph_sync_write_unsafe;
  666. req->r_inode = inode;
  667. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
  668. false, true);
  669. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  670. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  671. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  672. if (!ret)
  673. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  674. out:
  675. ceph_osdc_put_request(req);
  676. if (ret == 0) {
  677. pos += len;
  678. written += len;
  679. if (pos > i_size_read(inode)) {
  680. check_caps = ceph_inode_set_size(inode, pos);
  681. if (check_caps)
  682. ceph_check_caps(ceph_inode(inode),
  683. CHECK_CAPS_AUTHONLY,
  684. NULL);
  685. }
  686. } else
  687. break;
  688. }
  689. if (ret != -EOLDSNAPC && written > 0) {
  690. ret = written;
  691. iocb->ki_pos = pos;
  692. }
  693. return ret;
  694. }
  695. /*
  696. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  697. * Atomically grab references, so that those bits are not released
  698. * back to the MDS mid-read.
  699. *
  700. * Hmm, the sync read case isn't actually async... should it be?
  701. */
  702. static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov,
  703. unsigned long nr_segs, loff_t pos)
  704. {
  705. struct file *filp = iocb->ki_filp;
  706. struct ceph_file_info *fi = filp->private_data;
  707. size_t len = iocb->ki_nbytes;
  708. struct inode *inode = file_inode(filp);
  709. struct ceph_inode_info *ci = ceph_inode(inode);
  710. ssize_t ret;
  711. int want, got = 0;
  712. int checkeof = 0, read = 0;
  713. again:
  714. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  715. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
  716. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  717. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  718. else
  719. want = CEPH_CAP_FILE_CACHE;
  720. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, &got, -1);
  721. if (ret < 0)
  722. return ret;
  723. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  724. (iocb->ki_filp->f_flags & O_DIRECT) ||
  725. (fi->flags & CEPH_F_SYNC)) {
  726. struct iov_iter i;
  727. dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  728. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  729. ceph_cap_string(got));
  730. if (!read) {
  731. ret = generic_segment_checks(iov, &nr_segs,
  732. &len, VERIFY_WRITE);
  733. if (ret)
  734. goto out;
  735. }
  736. iov_iter_init(&i, iov, nr_segs, len, read);
  737. /* hmm, this isn't really async... */
  738. ret = ceph_sync_read(iocb, &i, &checkeof);
  739. } else {
  740. /*
  741. * We can't modify the content of iov,
  742. * so we only read from beginning.
  743. */
  744. if (read) {
  745. iocb->ki_pos = pos;
  746. len = iocb->ki_nbytes;
  747. read = 0;
  748. }
  749. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  750. inode, ceph_vinop(inode), pos, (unsigned)len,
  751. ceph_cap_string(got));
  752. ret = generic_file_aio_read(iocb, iov, nr_segs, pos);
  753. }
  754. out:
  755. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  756. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  757. ceph_put_cap_refs(ci, got);
  758. if (checkeof && ret >= 0) {
  759. int statret = ceph_do_getattr(inode,
  760. CEPH_STAT_CAP_SIZE);
  761. /* hit EOF or hole? */
  762. if (statret == 0 && iocb->ki_pos < inode->i_size &&
  763. ret < len) {
  764. dout("sync_read hit hole, ppos %lld < size %lld"
  765. ", reading more\n", iocb->ki_pos,
  766. inode->i_size);
  767. read += ret;
  768. len -= ret;
  769. checkeof = 0;
  770. goto again;
  771. }
  772. }
  773. if (ret >= 0)
  774. ret += read;
  775. return ret;
  776. }
  777. /*
  778. * Take cap references to avoid releasing caps to MDS mid-write.
  779. *
  780. * If we are synchronous, and write with an old snap context, the OSD
  781. * may return EOLDSNAPC. In that case, retry the write.. _after_
  782. * dropping our cap refs and allowing the pending snap to logically
  783. * complete _before_ this write occurs.
  784. *
  785. * If we are near ENOSPC, write synchronously.
  786. */
  787. static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
  788. unsigned long nr_segs, loff_t pos)
  789. {
  790. struct file *file = iocb->ki_filp;
  791. struct ceph_file_info *fi = file->private_data;
  792. struct inode *inode = file_inode(file);
  793. struct ceph_inode_info *ci = ceph_inode(inode);
  794. struct ceph_osd_client *osdc =
  795. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  796. ssize_t count, written = 0;
  797. int err, want, got;
  798. if (ceph_snap(inode) != CEPH_NOSNAP)
  799. return -EROFS;
  800. mutex_lock(&inode->i_mutex);
  801. err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
  802. if (err)
  803. goto out;
  804. /* We can write back this queue in page reclaim */
  805. current->backing_dev_info = file->f_mapping->backing_dev_info;
  806. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  807. if (err)
  808. goto out;
  809. if (count == 0)
  810. goto out;
  811. err = file_remove_suid(file);
  812. if (err)
  813. goto out;
  814. err = file_update_time(file);
  815. if (err)
  816. goto out;
  817. retry_snap:
  818. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) {
  819. err = -ENOSPC;
  820. goto out;
  821. }
  822. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  823. inode, ceph_vinop(inode), pos, count, inode->i_size);
  824. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  825. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  826. else
  827. want = CEPH_CAP_FILE_BUFFER;
  828. got = 0;
  829. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, pos + count);
  830. if (err < 0)
  831. goto out;
  832. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  833. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  834. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  835. (file->f_flags & O_DIRECT) || (fi->flags & CEPH_F_SYNC)) {
  836. mutex_unlock(&inode->i_mutex);
  837. if (file->f_flags & O_DIRECT)
  838. written = ceph_sync_direct_write(iocb, iov,
  839. nr_segs, count);
  840. else
  841. written = ceph_sync_write(iocb, iov, nr_segs, count);
  842. if (written == -EOLDSNAPC) {
  843. dout("aio_write %p %llx.%llx %llu~%u"
  844. "got EOLDSNAPC, retrying\n",
  845. inode, ceph_vinop(inode),
  846. pos, (unsigned)iov->iov_len);
  847. mutex_lock(&inode->i_mutex);
  848. goto retry_snap;
  849. }
  850. } else {
  851. loff_t old_size = inode->i_size;
  852. struct iov_iter from;
  853. /*
  854. * No need to acquire the i_truncate_mutex. Because
  855. * the MDS revokes Fwb caps before sending truncate
  856. * message to us. We can't get Fwb cap while there
  857. * are pending vmtruncate. So write and vmtruncate
  858. * can not run at the same time
  859. */
  860. iov_iter_init(&from, iov, nr_segs, count, 0);
  861. written = generic_perform_write(file, &from, pos);
  862. if (likely(written >= 0))
  863. iocb->ki_pos = pos + written;
  864. if (inode->i_size > old_size)
  865. ceph_fscache_update_objectsize(inode);
  866. mutex_unlock(&inode->i_mutex);
  867. }
  868. if (written >= 0) {
  869. int dirty;
  870. spin_lock(&ci->i_ceph_lock);
  871. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  872. spin_unlock(&ci->i_ceph_lock);
  873. if (dirty)
  874. __mark_inode_dirty(inode, dirty);
  875. }
  876. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  877. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  878. ceph_cap_string(got));
  879. ceph_put_cap_refs(ci, got);
  880. if (written >= 0 &&
  881. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) ||
  882. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
  883. err = vfs_fsync_range(file, pos, pos + written - 1, 1);
  884. if (err < 0)
  885. written = err;
  886. }
  887. goto out_unlocked;
  888. out:
  889. mutex_unlock(&inode->i_mutex);
  890. out_unlocked:
  891. current->backing_dev_info = NULL;
  892. return written ? written : err;
  893. }
  894. /*
  895. * llseek. be sure to verify file size on SEEK_END.
  896. */
  897. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  898. {
  899. struct inode *inode = file->f_mapping->host;
  900. int ret;
  901. mutex_lock(&inode->i_mutex);
  902. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  903. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  904. if (ret < 0) {
  905. offset = ret;
  906. goto out;
  907. }
  908. }
  909. switch (whence) {
  910. case SEEK_END:
  911. offset += inode->i_size;
  912. break;
  913. case SEEK_CUR:
  914. /*
  915. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  916. * position-querying operation. Avoid rewriting the "same"
  917. * f_pos value back to the file because a concurrent read(),
  918. * write() or lseek() might have altered it
  919. */
  920. if (offset == 0) {
  921. offset = file->f_pos;
  922. goto out;
  923. }
  924. offset += file->f_pos;
  925. break;
  926. case SEEK_DATA:
  927. if (offset >= inode->i_size) {
  928. ret = -ENXIO;
  929. goto out;
  930. }
  931. break;
  932. case SEEK_HOLE:
  933. if (offset >= inode->i_size) {
  934. ret = -ENXIO;
  935. goto out;
  936. }
  937. offset = inode->i_size;
  938. break;
  939. }
  940. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  941. out:
  942. mutex_unlock(&inode->i_mutex);
  943. return offset;
  944. }
  945. static inline void ceph_zero_partial_page(
  946. struct inode *inode, loff_t offset, unsigned size)
  947. {
  948. struct page *page;
  949. pgoff_t index = offset >> PAGE_CACHE_SHIFT;
  950. page = find_lock_page(inode->i_mapping, index);
  951. if (page) {
  952. wait_on_page_writeback(page);
  953. zero_user(page, offset & (PAGE_CACHE_SIZE - 1), size);
  954. unlock_page(page);
  955. page_cache_release(page);
  956. }
  957. }
  958. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  959. loff_t length)
  960. {
  961. loff_t nearly = round_up(offset, PAGE_CACHE_SIZE);
  962. if (offset < nearly) {
  963. loff_t size = nearly - offset;
  964. if (length < size)
  965. size = length;
  966. ceph_zero_partial_page(inode, offset, size);
  967. offset += size;
  968. length -= size;
  969. }
  970. if (length >= PAGE_CACHE_SIZE) {
  971. loff_t size = round_down(length, PAGE_CACHE_SIZE);
  972. truncate_pagecache_range(inode, offset, offset + size - 1);
  973. offset += size;
  974. length -= size;
  975. }
  976. if (length)
  977. ceph_zero_partial_page(inode, offset, length);
  978. }
  979. static int ceph_zero_partial_object(struct inode *inode,
  980. loff_t offset, loff_t *length)
  981. {
  982. struct ceph_inode_info *ci = ceph_inode(inode);
  983. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  984. struct ceph_osd_request *req;
  985. int ret = 0;
  986. loff_t zero = 0;
  987. int op;
  988. if (!length) {
  989. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  990. length = &zero;
  991. } else {
  992. op = CEPH_OSD_OP_ZERO;
  993. }
  994. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  995. ceph_vino(inode),
  996. offset, length,
  997. 1, op,
  998. CEPH_OSD_FLAG_WRITE |
  999. CEPH_OSD_FLAG_ONDISK,
  1000. NULL, 0, 0, false);
  1001. if (IS_ERR(req)) {
  1002. ret = PTR_ERR(req);
  1003. goto out;
  1004. }
  1005. ceph_osdc_build_request(req, offset, NULL, ceph_vino(inode).snap,
  1006. &inode->i_mtime);
  1007. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1008. if (!ret) {
  1009. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1010. if (ret == -ENOENT)
  1011. ret = 0;
  1012. }
  1013. ceph_osdc_put_request(req);
  1014. out:
  1015. return ret;
  1016. }
  1017. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  1018. {
  1019. int ret = 0;
  1020. struct ceph_inode_info *ci = ceph_inode(inode);
  1021. s32 stripe_unit = ceph_file_layout_su(ci->i_layout);
  1022. s32 stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
  1023. s32 object_size = ceph_file_layout_object_size(ci->i_layout);
  1024. u64 object_set_size = object_size * stripe_count;
  1025. u64 nearly, t;
  1026. /* round offset up to next period boundary */
  1027. nearly = offset + object_set_size - 1;
  1028. t = nearly;
  1029. nearly -= do_div(t, object_set_size);
  1030. while (length && offset < nearly) {
  1031. loff_t size = length;
  1032. ret = ceph_zero_partial_object(inode, offset, &size);
  1033. if (ret < 0)
  1034. return ret;
  1035. offset += size;
  1036. length -= size;
  1037. }
  1038. while (length >= object_set_size) {
  1039. int i;
  1040. loff_t pos = offset;
  1041. for (i = 0; i < stripe_count; ++i) {
  1042. ret = ceph_zero_partial_object(inode, pos, NULL);
  1043. if (ret < 0)
  1044. return ret;
  1045. pos += stripe_unit;
  1046. }
  1047. offset += object_set_size;
  1048. length -= object_set_size;
  1049. }
  1050. while (length) {
  1051. loff_t size = length;
  1052. ret = ceph_zero_partial_object(inode, offset, &size);
  1053. if (ret < 0)
  1054. return ret;
  1055. offset += size;
  1056. length -= size;
  1057. }
  1058. return ret;
  1059. }
  1060. static long ceph_fallocate(struct file *file, int mode,
  1061. loff_t offset, loff_t length)
  1062. {
  1063. struct ceph_file_info *fi = file->private_data;
  1064. struct inode *inode = file_inode(file);
  1065. struct ceph_inode_info *ci = ceph_inode(inode);
  1066. struct ceph_osd_client *osdc =
  1067. &ceph_inode_to_client(inode)->client->osdc;
  1068. int want, got = 0;
  1069. int dirty;
  1070. int ret = 0;
  1071. loff_t endoff = 0;
  1072. loff_t size;
  1073. if (!S_ISREG(inode->i_mode))
  1074. return -EOPNOTSUPP;
  1075. mutex_lock(&inode->i_mutex);
  1076. if (ceph_snap(inode) != CEPH_NOSNAP) {
  1077. ret = -EROFS;
  1078. goto unlock;
  1079. }
  1080. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) &&
  1081. !(mode & FALLOC_FL_PUNCH_HOLE)) {
  1082. ret = -ENOSPC;
  1083. goto unlock;
  1084. }
  1085. size = i_size_read(inode);
  1086. if (!(mode & FALLOC_FL_KEEP_SIZE))
  1087. endoff = offset + length;
  1088. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1089. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1090. else
  1091. want = CEPH_CAP_FILE_BUFFER;
  1092. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, endoff);
  1093. if (ret < 0)
  1094. goto unlock;
  1095. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1096. if (offset < size)
  1097. ceph_zero_pagecache_range(inode, offset, length);
  1098. ret = ceph_zero_objects(inode, offset, length);
  1099. } else if (endoff > size) {
  1100. truncate_pagecache_range(inode, size, -1);
  1101. if (ceph_inode_set_size(inode, endoff))
  1102. ceph_check_caps(ceph_inode(inode),
  1103. CHECK_CAPS_AUTHONLY, NULL);
  1104. }
  1105. if (!ret) {
  1106. spin_lock(&ci->i_ceph_lock);
  1107. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  1108. spin_unlock(&ci->i_ceph_lock);
  1109. if (dirty)
  1110. __mark_inode_dirty(inode, dirty);
  1111. }
  1112. ceph_put_cap_refs(ci, got);
  1113. unlock:
  1114. mutex_unlock(&inode->i_mutex);
  1115. return ret;
  1116. }
  1117. const struct file_operations ceph_file_fops = {
  1118. .open = ceph_open,
  1119. .release = ceph_release,
  1120. .llseek = ceph_llseek,
  1121. .read = do_sync_read,
  1122. .write = do_sync_write,
  1123. .aio_read = ceph_aio_read,
  1124. .aio_write = ceph_aio_write,
  1125. .mmap = ceph_mmap,
  1126. .fsync = ceph_fsync,
  1127. .lock = ceph_lock,
  1128. .flock = ceph_flock,
  1129. .splice_read = generic_file_splice_read,
  1130. .splice_write = generic_file_splice_write,
  1131. .unlocked_ioctl = ceph_ioctl,
  1132. .compat_ioctl = ceph_ioctl,
  1133. .fallocate = ceph_fallocate,
  1134. };