file.c 24 KB

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