file.c 22 KB

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