file.c 24 KB

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