file.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. #include "ceph_debug.h"
  2. #include <linux/sched.h>
  3. #include <linux/slab.h>
  4. #include <linux/file.h>
  5. #include <linux/namei.h>
  6. #include <linux/writeback.h>
  7. #include "super.h"
  8. #include "mds_client.h"
  9. /*
  10. * Ceph file operations
  11. *
  12. * Implement basic open/close functionality, and implement
  13. * read/write.
  14. *
  15. * We implement three modes of file I/O:
  16. * - buffered uses the generic_file_aio_{read,write} helpers
  17. *
  18. * - synchronous is used when there is multi-client read/write
  19. * sharing, avoids the page cache, and synchronously waits for an
  20. * ack from the OSD.
  21. *
  22. * - direct io takes the variant of the sync path that references
  23. * user pages directly.
  24. *
  25. * fsync() flushes and waits on dirty pages, but just queues metadata
  26. * for writeback: since the MDS can recover size and mtime there is no
  27. * need to wait for MDS acknowledgement.
  28. */
  29. /*
  30. * Prepare an open request. Preallocate ceph_cap to avoid an
  31. * inopportune ENOMEM later.
  32. */
  33. static struct ceph_mds_request *
  34. prepare_open_request(struct super_block *sb, int flags, int create_mode)
  35. {
  36. struct ceph_client *client = ceph_sb_to_client(sb);
  37. struct ceph_mds_client *mdsc = &client->mdsc;
  38. struct ceph_mds_request *req;
  39. int want_auth = USE_ANY_MDS;
  40. int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
  41. if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
  42. want_auth = USE_AUTH_MDS;
  43. req = ceph_mdsc_create_request(mdsc, op, want_auth);
  44. if (IS_ERR(req))
  45. goto out;
  46. req->r_fmode = ceph_flags_to_mode(flags);
  47. req->r_args.open.flags = cpu_to_le32(flags);
  48. req->r_args.open.mode = cpu_to_le32(create_mode);
  49. req->r_args.open.preferred = cpu_to_le32(-1);
  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_client *client = ceph_sb_to_client(inode->i_sb);
  108. struct ceph_mds_client *mdsc = &client->mdsc;
  109. struct ceph_mds_request *req;
  110. struct ceph_file_info *cf = file->private_data;
  111. struct inode *parent_inode = file->f_dentry->d_parent->d_inode;
  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(&inode->i_lock);
  132. __ceph_get_fmode(ci, fmode);
  133. spin_unlock(&inode->i_lock);
  134. return ceph_init_file(inode, file, fmode);
  135. }
  136. /*
  137. * No need to block if we have any caps. Update wanted set
  138. * asynchronously.
  139. */
  140. spin_lock(&inode->i_lock);
  141. if (__ceph_is_any_real_caps(ci)) {
  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(&inode->i_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(&inode->i_lock);
  159. return ceph_init_file(inode, file, fmode);
  160. }
  161. spin_unlock(&inode->i_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 = igrab(inode);
  169. req->r_num_caps = 1;
  170. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  171. if (!err)
  172. err = ceph_init_file(inode, file, req->r_fmode);
  173. ceph_mdsc_put_request(req);
  174. dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
  175. out:
  176. return err;
  177. }
  178. /*
  179. * Do a lookup + open with a single request.
  180. *
  181. * If this succeeds, but some subsequent check in the vfs
  182. * may_open() fails, the struct *file gets cleaned up (i.e.
  183. * ceph_release gets called). So fear not!
  184. */
  185. /*
  186. * flags
  187. * path_lookup_open -> LOOKUP_OPEN
  188. * path_lookup_create -> LOOKUP_OPEN|LOOKUP_CREATE
  189. */
  190. struct dentry *ceph_lookup_open(struct inode *dir, struct dentry *dentry,
  191. struct nameidata *nd, int mode,
  192. int locked_dir)
  193. {
  194. struct ceph_client *client = ceph_sb_to_client(dir->i_sb);
  195. struct ceph_mds_client *mdsc = &client->mdsc;
  196. struct file *file = nd->intent.open.file;
  197. struct inode *parent_inode = get_dentry_parent_inode(file->f_dentry);
  198. struct ceph_mds_request *req;
  199. int err;
  200. int flags = nd->intent.open.flags - 1; /* silly vfs! */
  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, parent_inode, req);
  215. dentry = ceph_finish_lookup(req, dentry, err);
  216. if (!err && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  217. err = ceph_handle_notrace_create(dir, dentry);
  218. if (!err)
  219. err = ceph_init_file(req->r_dentry->d_inode, file,
  220. req->r_fmode);
  221. ceph_mdsc_put_request(req);
  222. dout("ceph_lookup_open result=%p\n", dentry);
  223. return dentry;
  224. }
  225. int ceph_release(struct inode *inode, struct file *file)
  226. {
  227. struct ceph_inode_info *ci = ceph_inode(inode);
  228. struct ceph_file_info *cf = file->private_data;
  229. dout("release inode %p file %p\n", inode, file);
  230. ceph_put_fmode(ci, cf->fmode);
  231. if (cf->last_readdir)
  232. ceph_mdsc_put_request(cf->last_readdir);
  233. kfree(cf->last_name);
  234. kfree(cf->dir_info);
  235. dput(cf->dentry);
  236. kmem_cache_free(ceph_file_cachep, cf);
  237. /* wake up anyone waiting for caps on this inode */
  238. wake_up_all(&ci->i_cap_wq);
  239. return 0;
  240. }
  241. /*
  242. * build a vector of user pages
  243. */
  244. static struct page **get_direct_page_vector(const char __user *data,
  245. int num_pages,
  246. loff_t off, size_t len)
  247. {
  248. struct page **pages;
  249. int rc;
  250. pages = kmalloc(sizeof(*pages) * num_pages, GFP_NOFS);
  251. if (!pages)
  252. return ERR_PTR(-ENOMEM);
  253. down_read(&current->mm->mmap_sem);
  254. rc = get_user_pages(current, current->mm, (unsigned long)data,
  255. num_pages, 0, 0, pages, NULL);
  256. up_read(&current->mm->mmap_sem);
  257. if (rc < 0)
  258. goto fail;
  259. return pages;
  260. fail:
  261. kfree(pages);
  262. return ERR_PTR(rc);
  263. }
  264. static void put_page_vector(struct page **pages, int num_pages)
  265. {
  266. int i;
  267. for (i = 0; i < num_pages; i++)
  268. put_page(pages[i]);
  269. kfree(pages);
  270. }
  271. void ceph_release_page_vector(struct page **pages, int num_pages)
  272. {
  273. int i;
  274. for (i = 0; i < num_pages; i++)
  275. __free_pages(pages[i], 0);
  276. kfree(pages);
  277. }
  278. /*
  279. * allocate a vector new pages
  280. */
  281. static struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags)
  282. {
  283. struct page **pages;
  284. int i;
  285. pages = kmalloc(sizeof(*pages) * num_pages, flags);
  286. if (!pages)
  287. return ERR_PTR(-ENOMEM);
  288. for (i = 0; i < num_pages; i++) {
  289. pages[i] = __page_cache_alloc(flags);
  290. if (pages[i] == NULL) {
  291. ceph_release_page_vector(pages, i);
  292. return ERR_PTR(-ENOMEM);
  293. }
  294. }
  295. return pages;
  296. }
  297. /*
  298. * copy user data into a page vector
  299. */
  300. static int copy_user_to_page_vector(struct page **pages,
  301. const char __user *data,
  302. loff_t off, size_t len)
  303. {
  304. int i = 0;
  305. int po = off & ~PAGE_CACHE_MASK;
  306. int left = len;
  307. int l, bad;
  308. while (left > 0) {
  309. l = min_t(int, PAGE_CACHE_SIZE-po, left);
  310. bad = copy_from_user(page_address(pages[i]) + po, data, l);
  311. if (bad == l)
  312. return -EFAULT;
  313. data += l - bad;
  314. left -= l - bad;
  315. po += l - bad;
  316. if (po == PAGE_CACHE_SIZE) {
  317. po = 0;
  318. i++;
  319. }
  320. }
  321. return len;
  322. }
  323. /*
  324. * copy user data from a page vector into a user pointer
  325. */
  326. static int copy_page_vector_to_user(struct page **pages, char __user *data,
  327. loff_t off, size_t len)
  328. {
  329. int i = 0;
  330. int po = off & ~PAGE_CACHE_MASK;
  331. int left = len;
  332. int l, bad;
  333. while (left > 0) {
  334. l = min_t(int, left, PAGE_CACHE_SIZE-po);
  335. bad = copy_to_user(data, page_address(pages[i]) + po, l);
  336. if (bad == l)
  337. return -EFAULT;
  338. data += l - bad;
  339. left -= l - bad;
  340. if (po) {
  341. po += l - bad;
  342. if (po == PAGE_CACHE_SIZE)
  343. po = 0;
  344. }
  345. i++;
  346. }
  347. return len;
  348. }
  349. /*
  350. * Zero an extent within a page vector. Offset is relative to the
  351. * start of the first page.
  352. */
  353. static void zero_page_vector_range(int off, int len, struct page **pages)
  354. {
  355. int i = off >> PAGE_CACHE_SHIFT;
  356. off &= ~PAGE_CACHE_MASK;
  357. dout("zero_page_vector_page %u~%u\n", off, len);
  358. /* leading partial page? */
  359. if (off) {
  360. int end = min((int)PAGE_CACHE_SIZE, off + len);
  361. dout("zeroing %d %p head from %d\n", i, pages[i],
  362. (int)off);
  363. zero_user_segment(pages[i], off, end);
  364. len -= (end - off);
  365. i++;
  366. }
  367. while (len >= PAGE_CACHE_SIZE) {
  368. dout("zeroing %d %p len=%d\n", i, pages[i], len);
  369. zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
  370. len -= PAGE_CACHE_SIZE;
  371. i++;
  372. }
  373. /* trailing partial page? */
  374. if (len) {
  375. dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len);
  376. zero_user_segment(pages[i], 0, len);
  377. }
  378. }
  379. /*
  380. * Read a range of bytes striped over one or more objects. Iterate over
  381. * objects we stripe over. (That's not atomic, but good enough for now.)
  382. *
  383. * If we get a short result from the OSD, check against i_size; we need to
  384. * only return a short read to the caller if we hit EOF.
  385. */
  386. static int striped_read(struct inode *inode,
  387. u64 off, u64 len,
  388. struct page **pages, int num_pages,
  389. int *checkeof)
  390. {
  391. struct ceph_client *client = ceph_inode_to_client(inode);
  392. struct ceph_inode_info *ci = ceph_inode(inode);
  393. u64 pos, this_len;
  394. int page_off = off & ~PAGE_CACHE_MASK; /* first byte's offset in page */
  395. int left, pages_left;
  396. int read;
  397. struct page **page_pos;
  398. int ret;
  399. bool hit_stripe, was_short;
  400. /*
  401. * we may need to do multiple reads. not atomic, unfortunately.
  402. */
  403. pos = off;
  404. left = len;
  405. page_pos = pages;
  406. pages_left = num_pages;
  407. read = 0;
  408. more:
  409. this_len = left;
  410. ret = ceph_osdc_readpages(&client->osdc, ceph_vino(inode),
  411. &ci->i_layout, pos, &this_len,
  412. ci->i_truncate_seq,
  413. ci->i_truncate_size,
  414. page_pos, pages_left);
  415. hit_stripe = this_len < left;
  416. was_short = ret >= 0 && ret < this_len;
  417. if (ret == -ENOENT)
  418. ret = 0;
  419. dout("striped_read %llu~%u (read %u) got %d%s%s\n", pos, left, read,
  420. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  421. if (ret > 0) {
  422. int didpages =
  423. ((pos & ~PAGE_CACHE_MASK) + ret) >> PAGE_CACHE_SHIFT;
  424. if (read < pos - off) {
  425. dout(" zero gap %llu to %llu\n", off + read, pos);
  426. zero_page_vector_range(page_off + read,
  427. pos - off - read, pages);
  428. }
  429. pos += ret;
  430. read = pos - off;
  431. left -= ret;
  432. page_pos += didpages;
  433. pages_left -= didpages;
  434. /* hit stripe? */
  435. if (left && hit_stripe)
  436. goto more;
  437. }
  438. if (was_short) {
  439. /* was original extent fully inside i_size? */
  440. if (pos + left <= inode->i_size) {
  441. dout("zero tail\n");
  442. zero_page_vector_range(page_off + read, len - read,
  443. pages);
  444. read = len;
  445. goto out;
  446. }
  447. /* check i_size */
  448. *checkeof = 1;
  449. }
  450. out:
  451. if (ret >= 0)
  452. ret = read;
  453. dout("striped_read returns %d\n", ret);
  454. return ret;
  455. }
  456. /*
  457. * Completely synchronous read and write methods. Direct from __user
  458. * buffer to osd, or directly to user pages (if O_DIRECT).
  459. *
  460. * If the read spans object boundary, just do multiple reads.
  461. */
  462. static ssize_t ceph_sync_read(struct file *file, char __user *data,
  463. unsigned len, loff_t *poff, int *checkeof)
  464. {
  465. struct inode *inode = file->f_dentry->d_inode;
  466. struct page **pages;
  467. u64 off = *poff;
  468. int num_pages = calc_pages_for(off, len);
  469. int ret;
  470. dout("sync_read on file %p %llu~%u %s\n", file, off, len,
  471. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  472. if (file->f_flags & O_DIRECT) {
  473. pages = get_direct_page_vector(data, num_pages, off, len);
  474. /*
  475. * flush any page cache pages in this range. this
  476. * will make concurrent normal and O_DIRECT io slow,
  477. * but it will at least behave sensibly when they are
  478. * in sequence.
  479. */
  480. } else {
  481. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  482. }
  483. if (IS_ERR(pages))
  484. return PTR_ERR(pages);
  485. ret = filemap_write_and_wait(inode->i_mapping);
  486. if (ret < 0)
  487. goto done;
  488. ret = striped_read(inode, off, len, pages, num_pages, checkeof);
  489. if (ret >= 0 && (file->f_flags & O_DIRECT) == 0)
  490. ret = copy_page_vector_to_user(pages, data, off, ret);
  491. if (ret >= 0)
  492. *poff = off + ret;
  493. done:
  494. if (file->f_flags & O_DIRECT)
  495. put_page_vector(pages, num_pages);
  496. else
  497. ceph_release_page_vector(pages, num_pages);
  498. dout("sync_read result %d\n", ret);
  499. return ret;
  500. }
  501. /*
  502. * Write commit callback, called if we requested both an ACK and
  503. * ONDISK commit reply from the OSD.
  504. */
  505. static void sync_write_commit(struct ceph_osd_request *req,
  506. struct ceph_msg *msg)
  507. {
  508. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  509. dout("sync_write_commit %p tid %llu\n", req, req->r_tid);
  510. spin_lock(&ci->i_unsafe_lock);
  511. list_del_init(&req->r_unsafe_item);
  512. spin_unlock(&ci->i_unsafe_lock);
  513. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  514. }
  515. /*
  516. * Synchronous write, straight from __user pointer or user pages (if
  517. * O_DIRECT).
  518. *
  519. * If write spans object boundary, just do multiple writes. (For a
  520. * correct atomic write, we should e.g. take write locks on all
  521. * objects, rollback on failure, etc.)
  522. */
  523. static ssize_t ceph_sync_write(struct file *file, const char __user *data,
  524. size_t left, loff_t *offset)
  525. {
  526. struct inode *inode = file->f_dentry->d_inode;
  527. struct ceph_inode_info *ci = ceph_inode(inode);
  528. struct ceph_client *client = ceph_inode_to_client(inode);
  529. struct ceph_osd_request *req;
  530. struct page **pages;
  531. int num_pages;
  532. long long unsigned pos;
  533. u64 len;
  534. int written = 0;
  535. int flags;
  536. int do_sync = 0;
  537. int check_caps = 0;
  538. int ret;
  539. struct timespec mtime = CURRENT_TIME;
  540. if (ceph_snap(file->f_dentry->d_inode) != CEPH_NOSNAP)
  541. return -EROFS;
  542. dout("sync_write on file %p %lld~%u %s\n", file, *offset,
  543. (unsigned)left, (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  544. if (file->f_flags & O_APPEND)
  545. pos = i_size_read(inode);
  546. else
  547. pos = *offset;
  548. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + left);
  549. if (ret < 0)
  550. return ret;
  551. ret = invalidate_inode_pages2_range(inode->i_mapping,
  552. pos >> PAGE_CACHE_SHIFT,
  553. (pos + left) >> PAGE_CACHE_SHIFT);
  554. if (ret < 0)
  555. dout("invalidate_inode_pages2_range returned %d\n", ret);
  556. flags = CEPH_OSD_FLAG_ORDERSNAP |
  557. CEPH_OSD_FLAG_ONDISK |
  558. CEPH_OSD_FLAG_WRITE;
  559. if ((file->f_flags & (O_SYNC|O_DIRECT)) == 0)
  560. flags |= CEPH_OSD_FLAG_ACK;
  561. else
  562. do_sync = 1;
  563. /*
  564. * we may need to do multiple writes here if we span an object
  565. * boundary. this isn't atomic, unfortunately. :(
  566. */
  567. more:
  568. len = left;
  569. req = ceph_osdc_new_request(&client->osdc, &ci->i_layout,
  570. ceph_vino(inode), pos, &len,
  571. CEPH_OSD_OP_WRITE, flags,
  572. ci->i_snap_realm->cached_context,
  573. do_sync,
  574. ci->i_truncate_seq, ci->i_truncate_size,
  575. &mtime, false, 2);
  576. if (!req)
  577. return -ENOMEM;
  578. num_pages = calc_pages_for(pos, len);
  579. if (file->f_flags & O_DIRECT) {
  580. pages = get_direct_page_vector(data, num_pages, pos, len);
  581. if (IS_ERR(pages)) {
  582. ret = PTR_ERR(pages);
  583. goto out;
  584. }
  585. /*
  586. * throw out any page cache pages in this range. this
  587. * may block.
  588. */
  589. truncate_inode_pages_range(inode->i_mapping, pos,
  590. (pos+len) | (PAGE_CACHE_SIZE-1));
  591. } else {
  592. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  593. if (IS_ERR(pages)) {
  594. ret = PTR_ERR(pages);
  595. goto out;
  596. }
  597. ret = copy_user_to_page_vector(pages, data, pos, len);
  598. if (ret < 0) {
  599. ceph_release_page_vector(pages, num_pages);
  600. goto out;
  601. }
  602. if ((file->f_flags & O_SYNC) == 0) {
  603. /* get a second commit callback */
  604. req->r_safe_callback = sync_write_commit;
  605. req->r_own_pages = 1;
  606. }
  607. }
  608. req->r_pages = pages;
  609. req->r_num_pages = num_pages;
  610. req->r_inode = inode;
  611. ret = ceph_osdc_start_request(&client->osdc, req, false);
  612. if (!ret) {
  613. if (req->r_safe_callback) {
  614. /*
  615. * Add to inode unsafe list only after we
  616. * start_request so that a tid has been assigned.
  617. */
  618. spin_lock(&ci->i_unsafe_lock);
  619. list_add(&ci->i_unsafe_writes, &req->r_unsafe_item);
  620. spin_unlock(&ci->i_unsafe_lock);
  621. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  622. }
  623. ret = ceph_osdc_wait_request(&client->osdc, req);
  624. }
  625. if (file->f_flags & O_DIRECT)
  626. put_page_vector(pages, num_pages);
  627. else if (file->f_flags & O_SYNC)
  628. ceph_release_page_vector(pages, num_pages);
  629. out:
  630. ceph_osdc_put_request(req);
  631. if (ret == 0) {
  632. pos += len;
  633. written += len;
  634. left -= len;
  635. if (left)
  636. goto more;
  637. ret = written;
  638. *offset = pos;
  639. if (pos > i_size_read(inode))
  640. check_caps = ceph_inode_set_size(inode, pos);
  641. if (check_caps)
  642. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY,
  643. NULL);
  644. }
  645. return ret;
  646. }
  647. /*
  648. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  649. * Atomically grab references, so that those bits are not released
  650. * back to the MDS mid-read.
  651. *
  652. * Hmm, the sync read case isn't actually async... should it be?
  653. */
  654. static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov,
  655. unsigned long nr_segs, loff_t pos)
  656. {
  657. struct file *filp = iocb->ki_filp;
  658. struct ceph_file_info *fi = filp->private_data;
  659. loff_t *ppos = &iocb->ki_pos;
  660. size_t len = iov->iov_len;
  661. struct inode *inode = filp->f_dentry->d_inode;
  662. struct ceph_inode_info *ci = ceph_inode(inode);
  663. void __user *base = iov->iov_base;
  664. ssize_t ret;
  665. int want, got = 0;
  666. int checkeof = 0, read = 0;
  667. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  668. inode, ceph_vinop(inode), pos, (unsigned)len, inode);
  669. again:
  670. __ceph_do_pending_vmtruncate(inode);
  671. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  672. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  673. else
  674. want = CEPH_CAP_FILE_CACHE;
  675. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, &got, -1);
  676. if (ret < 0)
  677. goto out;
  678. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  679. inode, ceph_vinop(inode), pos, (unsigned)len,
  680. ceph_cap_string(got));
  681. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  682. (iocb->ki_filp->f_flags & O_DIRECT) ||
  683. (inode->i_sb->s_flags & MS_SYNCHRONOUS))
  684. /* hmm, this isn't really async... */
  685. ret = ceph_sync_read(filp, base, len, ppos, &checkeof);
  686. else
  687. ret = generic_file_aio_read(iocb, iov, nr_segs, pos);
  688. out:
  689. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  690. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  691. ceph_put_cap_refs(ci, got);
  692. if (checkeof && ret >= 0) {
  693. int statret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  694. /* hit EOF or hole? */
  695. if (statret == 0 && *ppos < inode->i_size) {
  696. dout("aio_read sync_read hit hole, reading more\n");
  697. read += ret;
  698. base += ret;
  699. len -= ret;
  700. checkeof = 0;
  701. goto again;
  702. }
  703. }
  704. if (ret >= 0)
  705. ret += read;
  706. return ret;
  707. }
  708. /*
  709. * Take cap references to avoid releasing caps to MDS mid-write.
  710. *
  711. * If we are synchronous, and write with an old snap context, the OSD
  712. * may return EOLDSNAPC. In that case, retry the write.. _after_
  713. * dropping our cap refs and allowing the pending snap to logically
  714. * complete _before_ this write occurs.
  715. *
  716. * If we are near ENOSPC, write synchronously.
  717. */
  718. static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
  719. unsigned long nr_segs, loff_t pos)
  720. {
  721. struct file *file = iocb->ki_filp;
  722. struct ceph_file_info *fi = file->private_data;
  723. struct inode *inode = file->f_dentry->d_inode;
  724. struct ceph_inode_info *ci = ceph_inode(inode);
  725. struct ceph_osd_client *osdc = &ceph_sb_to_client(inode->i_sb)->osdc;
  726. loff_t endoff = pos + iov->iov_len;
  727. int want, got = 0;
  728. int ret, err;
  729. if (ceph_snap(inode) != CEPH_NOSNAP)
  730. return -EROFS;
  731. retry_snap:
  732. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  733. return -ENOSPC;
  734. __ceph_do_pending_vmtruncate(inode);
  735. dout("aio_write %p %llx.%llx %llu~%u getting caps. i_size %llu\n",
  736. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  737. inode->i_size);
  738. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  739. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  740. else
  741. want = CEPH_CAP_FILE_BUFFER;
  742. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, endoff);
  743. if (ret < 0)
  744. goto out;
  745. dout("aio_write %p %llx.%llx %llu~%u got cap refs on %s\n",
  746. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  747. ceph_cap_string(got));
  748. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  749. (iocb->ki_filp->f_flags & O_DIRECT) ||
  750. (inode->i_sb->s_flags & MS_SYNCHRONOUS)) {
  751. ret = ceph_sync_write(file, iov->iov_base, iov->iov_len,
  752. &iocb->ki_pos);
  753. } else {
  754. ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
  755. if ((ret >= 0 || ret == -EIOCBQUEUED) &&
  756. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host)
  757. || ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
  758. err = vfs_fsync_range(file, pos, pos + ret - 1, 1);
  759. if (err < 0)
  760. ret = err;
  761. }
  762. }
  763. if (ret >= 0) {
  764. spin_lock(&inode->i_lock);
  765. __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  766. spin_unlock(&inode->i_lock);
  767. }
  768. out:
  769. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  770. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  771. ceph_cap_string(got));
  772. ceph_put_cap_refs(ci, got);
  773. if (ret == -EOLDSNAPC) {
  774. dout("aio_write %p %llx.%llx %llu~%u got EOLDSNAPC, retrying\n",
  775. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len);
  776. goto retry_snap;
  777. }
  778. return ret;
  779. }
  780. /*
  781. * llseek. be sure to verify file size on SEEK_END.
  782. */
  783. static loff_t ceph_llseek(struct file *file, loff_t offset, int origin)
  784. {
  785. struct inode *inode = file->f_mapping->host;
  786. int ret;
  787. mutex_lock(&inode->i_mutex);
  788. __ceph_do_pending_vmtruncate(inode);
  789. switch (origin) {
  790. case SEEK_END:
  791. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  792. if (ret < 0) {
  793. offset = ret;
  794. goto out;
  795. }
  796. offset += inode->i_size;
  797. break;
  798. case SEEK_CUR:
  799. /*
  800. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  801. * position-querying operation. Avoid rewriting the "same"
  802. * f_pos value back to the file because a concurrent read(),
  803. * write() or lseek() might have altered it
  804. */
  805. if (offset == 0) {
  806. offset = file->f_pos;
  807. goto out;
  808. }
  809. offset += file->f_pos;
  810. break;
  811. }
  812. if (offset < 0 || offset > inode->i_sb->s_maxbytes) {
  813. offset = -EINVAL;
  814. goto out;
  815. }
  816. /* Special lock needed here? */
  817. if (offset != file->f_pos) {
  818. file->f_pos = offset;
  819. file->f_version = 0;
  820. }
  821. out:
  822. mutex_unlock(&inode->i_mutex);
  823. return offset;
  824. }
  825. const struct file_operations ceph_file_fops = {
  826. .open = ceph_open,
  827. .release = ceph_release,
  828. .llseek = ceph_llseek,
  829. .read = do_sync_read,
  830. .write = do_sync_write,
  831. .aio_read = ceph_aio_read,
  832. .aio_write = ceph_aio_write,
  833. .mmap = ceph_mmap,
  834. .fsync = ceph_fsync,
  835. .lock = ceph_lock,
  836. .flock = ceph_flock,
  837. .splice_read = generic_file_splice_read,
  838. .splice_write = generic_file_splice_write,
  839. .unlocked_ioctl = ceph_ioctl,
  840. .compat_ioctl = ceph_ioctl,
  841. };