file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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 "super.h"
  11. #include "mds_client.h"
  12. /*
  13. * Ceph file operations
  14. *
  15. * Implement basic open/close functionality, and implement
  16. * read/write.
  17. *
  18. * We implement three modes of file I/O:
  19. * - buffered uses the generic_file_aio_{read,write} helpers
  20. *
  21. * - synchronous is used when there is multi-client read/write
  22. * sharing, avoids the page cache, and synchronously waits for an
  23. * ack from the OSD.
  24. *
  25. * - direct io takes the variant of the sync path that references
  26. * user pages directly.
  27. *
  28. * fsync() flushes and waits on dirty pages, but just queues metadata
  29. * for writeback: since the MDS can recover size and mtime there is no
  30. * need to wait for MDS acknowledgement.
  31. */
  32. /*
  33. * Prepare an open request. Preallocate ceph_cap to avoid an
  34. * inopportune ENOMEM later.
  35. */
  36. static struct ceph_mds_request *
  37. prepare_open_request(struct super_block *sb, int flags, int create_mode)
  38. {
  39. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  40. struct ceph_mds_client *mdsc = fsc->mdsc;
  41. struct ceph_mds_request *req;
  42. int want_auth = USE_ANY_MDS;
  43. int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
  44. if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
  45. want_auth = USE_AUTH_MDS;
  46. req = ceph_mdsc_create_request(mdsc, op, want_auth);
  47. if (IS_ERR(req))
  48. goto out;
  49. req->r_fmode = ceph_flags_to_mode(flags);
  50. req->r_args.open.flags = cpu_to_le32(flags);
  51. req->r_args.open.mode = cpu_to_le32(create_mode);
  52. out:
  53. return req;
  54. }
  55. /*
  56. * initialize private struct file data.
  57. * if we fail, clean up by dropping fmode reference on the ceph_inode
  58. */
  59. static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
  60. {
  61. struct ceph_file_info *cf;
  62. int ret = 0;
  63. switch (inode->i_mode & S_IFMT) {
  64. case S_IFREG:
  65. case S_IFDIR:
  66. dout("init_file %p %p 0%o (regular)\n", inode, file,
  67. inode->i_mode);
  68. cf = kmem_cache_alloc(ceph_file_cachep, GFP_NOFS | __GFP_ZERO);
  69. if (cf == NULL) {
  70. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  71. return -ENOMEM;
  72. }
  73. cf->fmode = fmode;
  74. cf->next_offset = 2;
  75. file->private_data = cf;
  76. BUG_ON(inode->i_fop->release != ceph_release);
  77. break;
  78. case S_IFLNK:
  79. dout("init_file %p %p 0%o (symlink)\n", inode, file,
  80. inode->i_mode);
  81. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  82. break;
  83. default:
  84. dout("init_file %p %p 0%o (special)\n", inode, file,
  85. inode->i_mode);
  86. /*
  87. * we need to drop the open ref now, since we don't
  88. * have .release set to ceph_release.
  89. */
  90. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  91. BUG_ON(inode->i_fop->release == ceph_release);
  92. /* call the proper open fop */
  93. ret = inode->i_fop->open(inode, file);
  94. }
  95. return ret;
  96. }
  97. /*
  98. * If we already have the requisite capabilities, we can satisfy
  99. * the open request locally (no need to request new caps from the
  100. * MDS). We do, however, need to inform the MDS (asynchronously)
  101. * if our wanted caps set expands.
  102. */
  103. int ceph_open(struct inode *inode, struct file *file)
  104. {
  105. struct ceph_inode_info *ci = ceph_inode(inode);
  106. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  107. struct ceph_mds_client *mdsc = fsc->mdsc;
  108. struct ceph_mds_request *req;
  109. struct ceph_file_info *cf = file->private_data;
  110. struct inode *parent_inode = NULL;
  111. int err;
  112. int flags, fmode, wanted;
  113. if (cf) {
  114. dout("open file %p is already opened\n", file);
  115. return 0;
  116. }
  117. /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
  118. flags = file->f_flags & ~(O_CREAT|O_EXCL);
  119. if (S_ISDIR(inode->i_mode))
  120. flags = O_DIRECTORY; /* mds likes to know */
  121. dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
  122. ceph_vinop(inode), file, flags, file->f_flags);
  123. fmode = ceph_flags_to_mode(flags);
  124. wanted = ceph_caps_for_mode(fmode);
  125. /* snapped files are read-only */
  126. if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
  127. return -EROFS;
  128. /* trivially open snapdir */
  129. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  130. spin_lock(&ci->i_ceph_lock);
  131. __ceph_get_fmode(ci, fmode);
  132. spin_unlock(&ci->i_ceph_lock);
  133. return ceph_init_file(inode, file, fmode);
  134. }
  135. /*
  136. * No need to block if we have caps on the auth MDS (for
  137. * write) or any MDS (for read). Update wanted set
  138. * asynchronously.
  139. */
  140. spin_lock(&ci->i_ceph_lock);
  141. if (__ceph_is_any_real_caps(ci) &&
  142. (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
  143. int mds_wanted = __ceph_caps_mds_wanted(ci);
  144. int issued = __ceph_caps_issued(ci, NULL);
  145. dout("open %p fmode %d want %s issued %s using existing\n",
  146. inode, fmode, ceph_cap_string(wanted),
  147. ceph_cap_string(issued));
  148. __ceph_get_fmode(ci, fmode);
  149. spin_unlock(&ci->i_ceph_lock);
  150. /* adjust wanted? */
  151. if ((issued & wanted) != wanted &&
  152. (mds_wanted & wanted) != wanted &&
  153. ceph_snap(inode) != CEPH_SNAPDIR)
  154. ceph_check_caps(ci, 0, NULL);
  155. return ceph_init_file(inode, file, fmode);
  156. } else if (ceph_snap(inode) != CEPH_NOSNAP &&
  157. (ci->i_snap_caps & wanted) == wanted) {
  158. __ceph_get_fmode(ci, fmode);
  159. spin_unlock(&ci->i_ceph_lock);
  160. return ceph_init_file(inode, file, fmode);
  161. }
  162. spin_unlock(&ci->i_ceph_lock);
  163. dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
  164. req = prepare_open_request(inode->i_sb, flags, 0);
  165. if (IS_ERR(req)) {
  166. err = PTR_ERR(req);
  167. goto out;
  168. }
  169. req->r_inode = inode;
  170. ihold(inode);
  171. req->r_num_caps = 1;
  172. if (flags & (O_CREAT|O_TRUNC))
  173. parent_inode = ceph_get_dentry_parent_inode(file->f_dentry);
  174. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  175. iput(parent_inode);
  176. if (!err)
  177. err = ceph_init_file(inode, file, req->r_fmode);
  178. ceph_mdsc_put_request(req);
  179. dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
  180. out:
  181. return err;
  182. }
  183. /*
  184. * Do a lookup + open with a single request. If we get a non-existent
  185. * file or symlink, return 1 so the VFS can retry.
  186. */
  187. int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
  188. struct file *file, unsigned flags, umode_t mode,
  189. int *opened)
  190. {
  191. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  192. struct ceph_mds_client *mdsc = fsc->mdsc;
  193. struct ceph_mds_request *req;
  194. struct dentry *dn;
  195. int err;
  196. dout("atomic_open %p dentry %p '%.*s' %s flags %d mode 0%o\n",
  197. dir, dentry, dentry->d_name.len, dentry->d_name.name,
  198. d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
  199. if (dentry->d_name.len > NAME_MAX)
  200. return -ENAMETOOLONG;
  201. err = ceph_init_dentry(dentry);
  202. if (err < 0)
  203. return err;
  204. /* do the open */
  205. req = prepare_open_request(dir->i_sb, flags, mode);
  206. if (IS_ERR(req))
  207. return PTR_ERR(req);
  208. req->r_dentry = dget(dentry);
  209. req->r_num_caps = 2;
  210. if (flags & O_CREAT) {
  211. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  212. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  213. }
  214. req->r_locked_dir = dir; /* caller holds dir->i_mutex */
  215. err = ceph_mdsc_do_request(mdsc,
  216. (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
  217. req);
  218. if (err)
  219. goto out_err;
  220. err = ceph_handle_snapdir(req, dentry, err);
  221. if (err == 0 && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  222. err = ceph_handle_notrace_create(dir, dentry);
  223. if (d_unhashed(dentry)) {
  224. dn = ceph_finish_lookup(req, dentry, err);
  225. if (IS_ERR(dn))
  226. err = PTR_ERR(dn);
  227. } else {
  228. /* we were given a hashed negative dentry */
  229. dn = NULL;
  230. }
  231. if (err)
  232. goto out_err;
  233. if (dn || dentry->d_inode == NULL || S_ISLNK(dentry->d_inode->i_mode)) {
  234. /* make vfs retry on splice, ENOENT, or symlink */
  235. dout("atomic_open finish_no_open on dn %p\n", dn);
  236. err = finish_no_open(file, dn);
  237. } else {
  238. dout("atomic_open finish_open on dn %p\n", dn);
  239. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  240. *opened |= FILE_CREATED;
  241. }
  242. err = finish_open(file, dentry, ceph_open, opened);
  243. }
  244. out_err:
  245. ceph_mdsc_put_request(req);
  246. dout("atomic_open result=%d\n", err);
  247. return err;
  248. }
  249. int ceph_release(struct inode *inode, struct file *file)
  250. {
  251. struct ceph_inode_info *ci = ceph_inode(inode);
  252. struct ceph_file_info *cf = file->private_data;
  253. dout("release inode %p file %p\n", inode, file);
  254. ceph_put_fmode(ci, cf->fmode);
  255. if (cf->last_readdir)
  256. ceph_mdsc_put_request(cf->last_readdir);
  257. kfree(cf->last_name);
  258. kfree(cf->dir_info);
  259. dput(cf->dentry);
  260. kmem_cache_free(ceph_file_cachep, cf);
  261. /* wake up anyone waiting for caps on this inode */
  262. wake_up_all(&ci->i_cap_wq);
  263. return 0;
  264. }
  265. /*
  266. * Read a range of bytes striped over one or more objects. Iterate over
  267. * objects we stripe over. (That's not atomic, but good enough for now.)
  268. *
  269. * If we get a short result from the OSD, check against i_size; we need to
  270. * only return a short read to the caller if we hit EOF.
  271. */
  272. static int striped_read(struct inode *inode,
  273. u64 off, u64 len,
  274. struct page **pages, int num_pages,
  275. int *checkeof, bool o_direct,
  276. unsigned long buf_align)
  277. {
  278. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  279. struct ceph_inode_info *ci = ceph_inode(inode);
  280. u64 pos, this_len;
  281. int io_align, page_align;
  282. int left, pages_left;
  283. int read;
  284. struct page **page_pos;
  285. int ret;
  286. bool hit_stripe, was_short;
  287. /*
  288. * we may need to do multiple reads. not atomic, unfortunately.
  289. */
  290. pos = off;
  291. left = len;
  292. page_pos = pages;
  293. pages_left = num_pages;
  294. read = 0;
  295. io_align = off & ~PAGE_MASK;
  296. more:
  297. if (o_direct)
  298. page_align = (pos - io_align + buf_align) & ~PAGE_MASK;
  299. else
  300. page_align = pos & ~PAGE_MASK;
  301. this_len = left;
  302. ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
  303. &ci->i_layout, pos, &this_len,
  304. ci->i_truncate_seq,
  305. ci->i_truncate_size,
  306. page_pos, pages_left, page_align);
  307. if (ret == -ENOENT)
  308. ret = 0;
  309. hit_stripe = this_len < left;
  310. was_short = ret >= 0 && ret < this_len;
  311. dout("striped_read %llu~%u (read %u) got %d%s%s\n", pos, left, read,
  312. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  313. if (ret > 0) {
  314. int didpages = (page_align + ret) >> PAGE_CACHE_SHIFT;
  315. if (read < pos - off) {
  316. dout(" zero gap %llu to %llu\n", off + read, pos);
  317. ceph_zero_page_vector_range(page_align + read,
  318. pos - off - read, pages);
  319. }
  320. pos += ret;
  321. read = pos - off;
  322. left -= ret;
  323. page_pos += didpages;
  324. pages_left -= didpages;
  325. /* hit stripe? */
  326. if (left && hit_stripe)
  327. goto more;
  328. }
  329. if (was_short) {
  330. /* did we bounce off eof? */
  331. if (pos + left > inode->i_size)
  332. *checkeof = 1;
  333. /* zero trailing bytes (inside i_size) */
  334. if (left > 0 && pos < inode->i_size) {
  335. if (pos + left > inode->i_size)
  336. left = inode->i_size - pos;
  337. dout("zero tail %d\n", left);
  338. ceph_zero_page_vector_range(page_align + read, left,
  339. pages);
  340. read += left;
  341. }
  342. }
  343. if (ret >= 0)
  344. ret = read;
  345. dout("striped_read returns %d\n", ret);
  346. return ret;
  347. }
  348. /*
  349. * Completely synchronous read and write methods. Direct from __user
  350. * buffer to osd, or directly to user pages (if O_DIRECT).
  351. *
  352. * If the read spans object boundary, just do multiple reads.
  353. */
  354. static ssize_t ceph_sync_read(struct file *file, char __user *data,
  355. unsigned len, loff_t *poff, int *checkeof)
  356. {
  357. struct inode *inode = file_inode(file);
  358. struct page **pages;
  359. u64 off = *poff;
  360. int num_pages, ret;
  361. dout("sync_read on file %p %llu~%u %s\n", file, off, len,
  362. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  363. if (file->f_flags & O_DIRECT) {
  364. num_pages = calc_pages_for((unsigned long)data, len);
  365. pages = ceph_get_direct_page_vector(data, num_pages, true);
  366. } else {
  367. num_pages = calc_pages_for(off, len);
  368. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  369. }
  370. if (IS_ERR(pages))
  371. return PTR_ERR(pages);
  372. /*
  373. * flush any page cache pages in this range. this
  374. * will make concurrent normal and sync io slow,
  375. * but it will at least behave sensibly when they are
  376. * in sequence.
  377. */
  378. ret = filemap_write_and_wait(inode->i_mapping);
  379. if (ret < 0)
  380. goto done;
  381. ret = striped_read(inode, off, len, pages, num_pages, checkeof,
  382. file->f_flags & O_DIRECT,
  383. (unsigned long)data & ~PAGE_MASK);
  384. if (ret >= 0 && (file->f_flags & O_DIRECT) == 0)
  385. ret = ceph_copy_page_vector_to_user(pages, data, off, ret);
  386. if (ret >= 0)
  387. *poff = off + ret;
  388. done:
  389. if (file->f_flags & O_DIRECT)
  390. ceph_put_page_vector(pages, num_pages, true);
  391. else
  392. ceph_release_page_vector(pages, num_pages);
  393. dout("sync_read result %d\n", ret);
  394. return ret;
  395. }
  396. /*
  397. * Write commit request unsafe callback, called to tell us when a
  398. * request is unsafe (that is, in flight--has been handed to the
  399. * messenger to send to its target osd). It is called again when
  400. * we've received a response message indicating the request is
  401. * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
  402. * is completed early (and unsuccessfully) due to a timeout or
  403. * interrupt.
  404. *
  405. * This is used if we requested both an ACK and ONDISK commit reply
  406. * from the OSD.
  407. */
  408. static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
  409. {
  410. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  411. dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
  412. unsafe ? "un" : "");
  413. if (unsafe) {
  414. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  415. spin_lock(&ci->i_unsafe_lock);
  416. list_add_tail(&req->r_unsafe_item,
  417. &ci->i_unsafe_writes);
  418. spin_unlock(&ci->i_unsafe_lock);
  419. } else {
  420. spin_lock(&ci->i_unsafe_lock);
  421. list_del_init(&req->r_unsafe_item);
  422. spin_unlock(&ci->i_unsafe_lock);
  423. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  424. }
  425. }
  426. /*
  427. * Synchronous write, straight from __user pointer or user pages (if
  428. * O_DIRECT).
  429. *
  430. * If write spans object boundary, just do multiple writes. (For a
  431. * correct atomic write, we should e.g. take write locks on all
  432. * objects, rollback on failure, etc.)
  433. */
  434. static ssize_t ceph_sync_write(struct file *file, const char __user *data,
  435. size_t left, loff_t pos, loff_t *ppos)
  436. {
  437. struct inode *inode = file_inode(file);
  438. struct ceph_inode_info *ci = ceph_inode(inode);
  439. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  440. struct ceph_snap_context *snapc;
  441. struct ceph_vino vino;
  442. struct ceph_osd_request *req;
  443. int num_ops = 1;
  444. struct page **pages;
  445. int num_pages;
  446. u64 len;
  447. int written = 0;
  448. int flags;
  449. int check_caps = 0;
  450. int page_align, io_align;
  451. unsigned long buf_align;
  452. int ret;
  453. struct timespec mtime = CURRENT_TIME;
  454. bool own_pages = false;
  455. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  456. return -EROFS;
  457. dout("sync_write on file %p %lld~%u %s\n", file, pos,
  458. (unsigned)left, (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  459. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + left);
  460. if (ret < 0)
  461. return ret;
  462. ret = invalidate_inode_pages2_range(inode->i_mapping,
  463. pos >> PAGE_CACHE_SHIFT,
  464. (pos + left) >> PAGE_CACHE_SHIFT);
  465. if (ret < 0)
  466. dout("invalidate_inode_pages2_range returned %d\n", ret);
  467. flags = CEPH_OSD_FLAG_ORDERSNAP |
  468. CEPH_OSD_FLAG_ONDISK |
  469. CEPH_OSD_FLAG_WRITE;
  470. if ((file->f_flags & (O_SYNC|O_DIRECT)) == 0)
  471. flags |= CEPH_OSD_FLAG_ACK;
  472. else
  473. num_ops++; /* Also include a 'startsync' command. */
  474. /*
  475. * we may need to do multiple writes here if we span an object
  476. * boundary. this isn't atomic, unfortunately. :(
  477. */
  478. more:
  479. io_align = pos & ~PAGE_MASK;
  480. buf_align = (unsigned long)data & ~PAGE_MASK;
  481. len = left;
  482. snapc = ci->i_snap_realm->cached_context;
  483. vino = ceph_vino(inode);
  484. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  485. vino, pos, &len, num_ops,
  486. CEPH_OSD_OP_WRITE, flags, snapc,
  487. ci->i_truncate_seq, ci->i_truncate_size,
  488. false);
  489. if (IS_ERR(req))
  490. return PTR_ERR(req);
  491. /* write from beginning of first page, regardless of io alignment */
  492. page_align = file->f_flags & O_DIRECT ? buf_align : io_align;
  493. num_pages = calc_pages_for(page_align, len);
  494. if (file->f_flags & O_DIRECT) {
  495. pages = ceph_get_direct_page_vector(data, num_pages, false);
  496. if (IS_ERR(pages)) {
  497. ret = PTR_ERR(pages);
  498. goto out;
  499. }
  500. /*
  501. * throw out any page cache pages in this range. this
  502. * may block.
  503. */
  504. truncate_inode_pages_range(inode->i_mapping, pos,
  505. (pos+len) | (PAGE_CACHE_SIZE-1));
  506. } else {
  507. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  508. if (IS_ERR(pages)) {
  509. ret = PTR_ERR(pages);
  510. goto out;
  511. }
  512. ret = ceph_copy_user_to_page_vector(pages, data, pos, len);
  513. if (ret < 0) {
  514. ceph_release_page_vector(pages, num_pages);
  515. goto out;
  516. }
  517. if ((file->f_flags & O_SYNC) == 0) {
  518. /* get a second commit callback */
  519. req->r_unsafe_callback = ceph_sync_write_unsafe;
  520. req->r_inode = inode;
  521. own_pages = true;
  522. }
  523. }
  524. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
  525. false, own_pages);
  526. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  527. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  528. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  529. if (!ret)
  530. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  531. if (file->f_flags & O_DIRECT)
  532. ceph_put_page_vector(pages, num_pages, false);
  533. else if (file->f_flags & O_SYNC)
  534. ceph_release_page_vector(pages, num_pages);
  535. out:
  536. ceph_osdc_put_request(req);
  537. if (ret == 0) {
  538. pos += len;
  539. written += len;
  540. left -= len;
  541. data += len;
  542. if (left)
  543. goto more;
  544. ret = written;
  545. *ppos = pos;
  546. if (pos > i_size_read(inode))
  547. check_caps = ceph_inode_set_size(inode, pos);
  548. if (check_caps)
  549. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY,
  550. NULL);
  551. }
  552. return ret;
  553. }
  554. /*
  555. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  556. * Atomically grab references, so that those bits are not released
  557. * back to the MDS mid-read.
  558. *
  559. * Hmm, the sync read case isn't actually async... should it be?
  560. */
  561. static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov,
  562. unsigned long nr_segs, loff_t pos)
  563. {
  564. struct file *filp = iocb->ki_filp;
  565. struct ceph_file_info *fi = filp->private_data;
  566. loff_t *ppos = &iocb->ki_pos;
  567. size_t len = iov->iov_len;
  568. struct inode *inode = file_inode(filp);
  569. struct ceph_inode_info *ci = ceph_inode(inode);
  570. void __user *base = iov->iov_base;
  571. ssize_t ret;
  572. int want, got = 0;
  573. int checkeof = 0, read = 0;
  574. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  575. inode, ceph_vinop(inode), pos, (unsigned)len, inode);
  576. again:
  577. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  578. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  579. else
  580. want = CEPH_CAP_FILE_CACHE;
  581. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, &got, -1);
  582. if (ret < 0)
  583. goto out;
  584. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  585. inode, ceph_vinop(inode), pos, (unsigned)len,
  586. ceph_cap_string(got));
  587. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  588. (iocb->ki_filp->f_flags & O_DIRECT) ||
  589. (inode->i_sb->s_flags & MS_SYNCHRONOUS) ||
  590. (fi->flags & CEPH_F_SYNC))
  591. /* hmm, this isn't really async... */
  592. ret = ceph_sync_read(filp, base, len, ppos, &checkeof);
  593. else
  594. ret = generic_file_aio_read(iocb, iov, nr_segs, pos);
  595. out:
  596. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  597. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  598. ceph_put_cap_refs(ci, got);
  599. if (checkeof && ret >= 0) {
  600. int statret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  601. /* hit EOF or hole? */
  602. if (statret == 0 && *ppos < inode->i_size) {
  603. dout("aio_read sync_read hit hole, ppos %lld < size %lld, reading more\n", *ppos, inode->i_size);
  604. read += ret;
  605. base += ret;
  606. len -= ret;
  607. checkeof = 0;
  608. goto again;
  609. }
  610. }
  611. if (ret >= 0)
  612. ret += read;
  613. return ret;
  614. }
  615. /*
  616. * Take cap references to avoid releasing caps to MDS mid-write.
  617. *
  618. * If we are synchronous, and write with an old snap context, the OSD
  619. * may return EOLDSNAPC. In that case, retry the write.. _after_
  620. * dropping our cap refs and allowing the pending snap to logically
  621. * complete _before_ this write occurs.
  622. *
  623. * If we are near ENOSPC, write synchronously.
  624. */
  625. static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
  626. unsigned long nr_segs, loff_t pos)
  627. {
  628. struct file *file = iocb->ki_filp;
  629. struct ceph_file_info *fi = file->private_data;
  630. struct inode *inode = file_inode(file);
  631. struct ceph_inode_info *ci = ceph_inode(inode);
  632. struct ceph_osd_client *osdc =
  633. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  634. ssize_t count, written = 0;
  635. int err, want, got;
  636. bool hold_mutex;
  637. if (ceph_snap(inode) != CEPH_NOSNAP)
  638. return -EROFS;
  639. mutex_lock(&inode->i_mutex);
  640. hold_mutex = true;
  641. err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
  642. if (err)
  643. goto out;
  644. /* We can write back this queue in page reclaim */
  645. current->backing_dev_info = file->f_mapping->backing_dev_info;
  646. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  647. if (err)
  648. goto out;
  649. if (count == 0)
  650. goto out;
  651. err = file_remove_suid(file);
  652. if (err)
  653. goto out;
  654. err = file_update_time(file);
  655. if (err)
  656. goto out;
  657. retry_snap:
  658. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) {
  659. err = -ENOSPC;
  660. goto out;
  661. }
  662. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  663. inode, ceph_vinop(inode), pos, count, inode->i_size);
  664. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  665. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  666. else
  667. want = CEPH_CAP_FILE_BUFFER;
  668. got = 0;
  669. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, pos + count);
  670. if (err < 0)
  671. goto out;
  672. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  673. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  674. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  675. (iocb->ki_filp->f_flags & O_DIRECT) ||
  676. (inode->i_sb->s_flags & MS_SYNCHRONOUS) ||
  677. (fi->flags & CEPH_F_SYNC)) {
  678. mutex_unlock(&inode->i_mutex);
  679. written = ceph_sync_write(file, iov->iov_base, count,
  680. pos, &iocb->ki_pos);
  681. } else {
  682. written = generic_file_buffered_write(iocb, iov, nr_segs,
  683. pos, &iocb->ki_pos,
  684. count, 0);
  685. mutex_unlock(&inode->i_mutex);
  686. }
  687. hold_mutex = false;
  688. if (written >= 0) {
  689. int dirty;
  690. spin_lock(&ci->i_ceph_lock);
  691. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  692. spin_unlock(&ci->i_ceph_lock);
  693. if (dirty)
  694. __mark_inode_dirty(inode, dirty);
  695. }
  696. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  697. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  698. ceph_cap_string(got));
  699. ceph_put_cap_refs(ci, got);
  700. if (written >= 0 &&
  701. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) ||
  702. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
  703. err = vfs_fsync_range(file, pos, pos + written - 1, 1);
  704. if (err < 0)
  705. written = err;
  706. }
  707. if (written == -EOLDSNAPC) {
  708. dout("aio_write %p %llx.%llx %llu~%u got EOLDSNAPC, retrying\n",
  709. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len);
  710. mutex_lock(&inode->i_mutex);
  711. hold_mutex = true;
  712. goto retry_snap;
  713. }
  714. out:
  715. if (hold_mutex)
  716. mutex_unlock(&inode->i_mutex);
  717. current->backing_dev_info = NULL;
  718. return written ? written : err;
  719. }
  720. /*
  721. * llseek. be sure to verify file size on SEEK_END.
  722. */
  723. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  724. {
  725. struct inode *inode = file->f_mapping->host;
  726. int ret;
  727. mutex_lock(&inode->i_mutex);
  728. __ceph_do_pending_vmtruncate(inode);
  729. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  730. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  731. if (ret < 0) {
  732. offset = ret;
  733. goto out;
  734. }
  735. }
  736. switch (whence) {
  737. case SEEK_END:
  738. offset += inode->i_size;
  739. break;
  740. case SEEK_CUR:
  741. /*
  742. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  743. * position-querying operation. Avoid rewriting the "same"
  744. * f_pos value back to the file because a concurrent read(),
  745. * write() or lseek() might have altered it
  746. */
  747. if (offset == 0) {
  748. offset = file->f_pos;
  749. goto out;
  750. }
  751. offset += file->f_pos;
  752. break;
  753. case SEEK_DATA:
  754. if (offset >= inode->i_size) {
  755. ret = -ENXIO;
  756. goto out;
  757. }
  758. break;
  759. case SEEK_HOLE:
  760. if (offset >= inode->i_size) {
  761. ret = -ENXIO;
  762. goto out;
  763. }
  764. offset = inode->i_size;
  765. break;
  766. }
  767. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  768. out:
  769. mutex_unlock(&inode->i_mutex);
  770. return offset;
  771. }
  772. const struct file_operations ceph_file_fops = {
  773. .open = ceph_open,
  774. .release = ceph_release,
  775. .llseek = ceph_llseek,
  776. .read = do_sync_read,
  777. .write = do_sync_write,
  778. .aio_read = ceph_aio_read,
  779. .aio_write = ceph_aio_write,
  780. .mmap = ceph_mmap,
  781. .fsync = ceph_fsync,
  782. .lock = ceph_lock,
  783. .flock = ceph_flock,
  784. .splice_read = generic_file_splice_read,
  785. .splice_write = generic_file_splice_write,
  786. .unlocked_ioctl = ceph_ioctl,
  787. .compat_ioctl = ceph_ioctl,
  788. };