file.c 24 KB

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