file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. return 0;
  237. }
  238. /*
  239. * build a vector of user pages
  240. */
  241. static struct page **get_direct_page_vector(const char __user *data,
  242. int num_pages,
  243. loff_t off, size_t len)
  244. {
  245. struct page **pages;
  246. int rc;
  247. pages = kmalloc(sizeof(*pages) * num_pages, GFP_NOFS);
  248. if (!pages)
  249. return ERR_PTR(-ENOMEM);
  250. down_read(&current->mm->mmap_sem);
  251. rc = get_user_pages(current, current->mm, (unsigned long)data,
  252. num_pages, 0, 0, pages, NULL);
  253. up_read(&current->mm->mmap_sem);
  254. if (rc < 0)
  255. goto fail;
  256. return pages;
  257. fail:
  258. kfree(pages);
  259. return ERR_PTR(rc);
  260. }
  261. static void put_page_vector(struct page **pages, int num_pages)
  262. {
  263. int i;
  264. for (i = 0; i < num_pages; i++)
  265. put_page(pages[i]);
  266. kfree(pages);
  267. }
  268. void ceph_release_page_vector(struct page **pages, int num_pages)
  269. {
  270. int i;
  271. for (i = 0; i < num_pages; i++)
  272. __free_pages(pages[i], 0);
  273. kfree(pages);
  274. }
  275. /*
  276. * allocate a vector new pages
  277. */
  278. static struct page **alloc_page_vector(int num_pages)
  279. {
  280. struct page **pages;
  281. int i;
  282. pages = kmalloc(sizeof(*pages) * num_pages, GFP_NOFS);
  283. if (!pages)
  284. return ERR_PTR(-ENOMEM);
  285. for (i = 0; i < num_pages; i++) {
  286. pages[i] = alloc_page(GFP_NOFS);
  287. if (pages[i] == NULL) {
  288. ceph_release_page_vector(pages, i);
  289. return ERR_PTR(-ENOMEM);
  290. }
  291. }
  292. return pages;
  293. }
  294. /*
  295. * copy user data into a page vector
  296. */
  297. static int copy_user_to_page_vector(struct page **pages,
  298. const char __user *data,
  299. loff_t off, size_t len)
  300. {
  301. int i = 0;
  302. int po = off & ~PAGE_CACHE_MASK;
  303. int left = len;
  304. int l, bad;
  305. while (left > 0) {
  306. l = min_t(int, PAGE_CACHE_SIZE-po, left);
  307. bad = copy_from_user(page_address(pages[i]) + po, data, l);
  308. if (bad == l)
  309. return -EFAULT;
  310. data += l - bad;
  311. left -= l - bad;
  312. po += l - bad;
  313. if (po == PAGE_CACHE_SIZE) {
  314. po = 0;
  315. i++;
  316. }
  317. }
  318. return len;
  319. }
  320. /*
  321. * copy user data from a page vector into a user pointer
  322. */
  323. static int copy_page_vector_to_user(struct page **pages, char __user *data,
  324. loff_t off, size_t len)
  325. {
  326. int i = 0;
  327. int po = off & ~PAGE_CACHE_MASK;
  328. int left = len;
  329. int l, bad;
  330. while (left > 0) {
  331. l = min_t(int, left, PAGE_CACHE_SIZE-po);
  332. bad = copy_to_user(data, page_address(pages[i]) + po, l);
  333. if (bad == l)
  334. return -EFAULT;
  335. data += l - bad;
  336. left -= l - bad;
  337. if (po) {
  338. po += l - bad;
  339. if (po == PAGE_CACHE_SIZE)
  340. po = 0;
  341. }
  342. i++;
  343. }
  344. return len;
  345. }
  346. /*
  347. * Zero an extent within a page vector. Offset is relative to the
  348. * start of the first page.
  349. */
  350. static void zero_page_vector_range(int off, int len, struct page **pages)
  351. {
  352. int i = off >> PAGE_CACHE_SHIFT;
  353. off &= ~PAGE_CACHE_MASK;
  354. dout("zero_page_vector_page %u~%u\n", off, len);
  355. /* leading partial page? */
  356. if (off) {
  357. int end = min((int)PAGE_CACHE_SIZE, off + len);
  358. dout("zeroing %d %p head from %d\n", i, pages[i],
  359. (int)off);
  360. zero_user_segment(pages[i], off, end);
  361. len -= (end - off);
  362. i++;
  363. }
  364. while (len >= PAGE_CACHE_SIZE) {
  365. dout("zeroing %d %p len=%d\n", i, pages[i], len);
  366. zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
  367. len -= PAGE_CACHE_SIZE;
  368. i++;
  369. }
  370. /* trailing partial page? */
  371. if (len) {
  372. dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len);
  373. zero_user_segment(pages[i], 0, len);
  374. }
  375. }
  376. /*
  377. * Read a range of bytes striped over one or more objects. Iterate over
  378. * objects we stripe over. (That's not atomic, but good enough for now.)
  379. *
  380. * If we get a short result from the OSD, check against i_size; we need to
  381. * only return a short read to the caller if we hit EOF.
  382. */
  383. static int striped_read(struct inode *inode,
  384. u64 off, u64 len,
  385. struct page **pages, int num_pages,
  386. int *checkeof)
  387. {
  388. struct ceph_client *client = ceph_inode_to_client(inode);
  389. struct ceph_inode_info *ci = ceph_inode(inode);
  390. u64 pos, this_len;
  391. int page_off = off & ~PAGE_CACHE_MASK; /* first byte's offset in page */
  392. int left, pages_left;
  393. int read;
  394. struct page **page_pos;
  395. int ret;
  396. bool hit_stripe, was_short;
  397. /*
  398. * we may need to do multiple reads. not atomic, unfortunately.
  399. */
  400. pos = off;
  401. left = len;
  402. page_pos = pages;
  403. pages_left = num_pages;
  404. read = 0;
  405. more:
  406. this_len = left;
  407. ret = ceph_osdc_readpages(&client->osdc, ceph_vino(inode),
  408. &ci->i_layout, pos, &this_len,
  409. ci->i_truncate_seq,
  410. ci->i_truncate_size,
  411. page_pos, pages_left);
  412. hit_stripe = this_len < left;
  413. was_short = ret >= 0 && ret < this_len;
  414. if (ret == -ENOENT)
  415. ret = 0;
  416. dout("striped_read %llu~%u (read %u) got %d%s%s\n", pos, left, read,
  417. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  418. if (ret > 0) {
  419. int didpages =
  420. ((pos & ~PAGE_CACHE_MASK) + ret) >> PAGE_CACHE_SHIFT;
  421. if (read < pos - off) {
  422. dout(" zero gap %llu to %llu\n", off + read, pos);
  423. zero_page_vector_range(page_off + read,
  424. pos - off - read, pages);
  425. }
  426. pos += ret;
  427. read = pos - off;
  428. left -= ret;
  429. page_pos += didpages;
  430. pages_left -= didpages;
  431. /* hit stripe? */
  432. if (left && hit_stripe)
  433. goto more;
  434. }
  435. if (was_short) {
  436. /* was original extent fully inside i_size? */
  437. if (pos + left <= inode->i_size) {
  438. dout("zero tail\n");
  439. zero_page_vector_range(page_off + read, len - read,
  440. pages);
  441. read = len;
  442. goto out;
  443. }
  444. /* check i_size */
  445. *checkeof = 1;
  446. }
  447. out:
  448. if (ret >= 0)
  449. ret = read;
  450. dout("striped_read returns %d\n", ret);
  451. return ret;
  452. }
  453. /*
  454. * Completely synchronous read and write methods. Direct from __user
  455. * buffer to osd, or directly to user pages (if O_DIRECT).
  456. *
  457. * If the read spans object boundary, just do multiple reads.
  458. */
  459. static ssize_t ceph_sync_read(struct file *file, char __user *data,
  460. unsigned len, loff_t *poff, int *checkeof)
  461. {
  462. struct inode *inode = file->f_dentry->d_inode;
  463. struct page **pages;
  464. u64 off = *poff;
  465. int num_pages = calc_pages_for(off, len);
  466. int ret;
  467. dout("sync_read on file %p %llu~%u %s\n", file, off, len,
  468. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  469. if (file->f_flags & O_DIRECT) {
  470. pages = get_direct_page_vector(data, num_pages, off, len);
  471. /*
  472. * flush any page cache pages in this range. this
  473. * will make concurrent normal and O_DIRECT io slow,
  474. * but it will at least behave sensibly when they are
  475. * in sequence.
  476. */
  477. } else {
  478. pages = alloc_page_vector(num_pages);
  479. }
  480. if (IS_ERR(pages))
  481. return PTR_ERR(pages);
  482. ret = filemap_write_and_wait(inode->i_mapping);
  483. if (ret < 0)
  484. goto done;
  485. ret = striped_read(inode, off, len, pages, num_pages, checkeof);
  486. if (ret >= 0 && (file->f_flags & O_DIRECT) == 0)
  487. ret = copy_page_vector_to_user(pages, data, off, ret);
  488. if (ret >= 0)
  489. *poff = off + ret;
  490. done:
  491. if (file->f_flags & O_DIRECT)
  492. put_page_vector(pages, num_pages);
  493. else
  494. ceph_release_page_vector(pages, num_pages);
  495. dout("sync_read result %d\n", ret);
  496. return ret;
  497. }
  498. /*
  499. * Write commit callback, called if we requested both an ACK and
  500. * ONDISK commit reply from the OSD.
  501. */
  502. static void sync_write_commit(struct ceph_osd_request *req,
  503. struct ceph_msg *msg)
  504. {
  505. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  506. dout("sync_write_commit %p tid %llu\n", req, req->r_tid);
  507. spin_lock(&ci->i_unsafe_lock);
  508. list_del_init(&req->r_unsafe_item);
  509. spin_unlock(&ci->i_unsafe_lock);
  510. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  511. }
  512. /*
  513. * Synchronous write, straight from __user pointer or user pages (if
  514. * O_DIRECT).
  515. *
  516. * If write spans object boundary, just do multiple writes. (For a
  517. * correct atomic write, we should e.g. take write locks on all
  518. * objects, rollback on failure, etc.)
  519. */
  520. static ssize_t ceph_sync_write(struct file *file, const char __user *data,
  521. size_t left, loff_t *offset)
  522. {
  523. struct inode *inode = file->f_dentry->d_inode;
  524. struct ceph_inode_info *ci = ceph_inode(inode);
  525. struct ceph_client *client = ceph_inode_to_client(inode);
  526. struct ceph_osd_request *req;
  527. struct page **pages;
  528. int num_pages;
  529. long long unsigned pos;
  530. u64 len;
  531. int written = 0;
  532. int flags;
  533. int do_sync = 0;
  534. int check_caps = 0;
  535. int ret;
  536. struct timespec mtime = CURRENT_TIME;
  537. if (ceph_snap(file->f_dentry->d_inode) != CEPH_NOSNAP)
  538. return -EROFS;
  539. dout("sync_write on file %p %lld~%u %s\n", file, *offset,
  540. (unsigned)left, (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  541. if (file->f_flags & O_APPEND)
  542. pos = i_size_read(inode);
  543. else
  544. pos = *offset;
  545. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + left);
  546. if (ret < 0)
  547. return ret;
  548. ret = invalidate_inode_pages2_range(inode->i_mapping,
  549. pos >> PAGE_CACHE_SHIFT,
  550. (pos + left) >> PAGE_CACHE_SHIFT);
  551. if (ret < 0)
  552. dout("invalidate_inode_pages2_range returned %d\n", ret);
  553. flags = CEPH_OSD_FLAG_ORDERSNAP |
  554. CEPH_OSD_FLAG_ONDISK |
  555. CEPH_OSD_FLAG_WRITE;
  556. if ((file->f_flags & (O_SYNC|O_DIRECT)) == 0)
  557. flags |= CEPH_OSD_FLAG_ACK;
  558. else
  559. do_sync = 1;
  560. /*
  561. * we may need to do multiple writes here if we span an object
  562. * boundary. this isn't atomic, unfortunately. :(
  563. */
  564. more:
  565. len = left;
  566. req = ceph_osdc_new_request(&client->osdc, &ci->i_layout,
  567. ceph_vino(inode), pos, &len,
  568. CEPH_OSD_OP_WRITE, flags,
  569. ci->i_snap_realm->cached_context,
  570. do_sync,
  571. ci->i_truncate_seq, ci->i_truncate_size,
  572. &mtime, false, 2);
  573. if (IS_ERR(req))
  574. return PTR_ERR(req);
  575. num_pages = calc_pages_for(pos, len);
  576. if (file->f_flags & O_DIRECT) {
  577. pages = get_direct_page_vector(data, num_pages, pos, len);
  578. if (IS_ERR(pages)) {
  579. ret = PTR_ERR(pages);
  580. goto out;
  581. }
  582. /*
  583. * throw out any page cache pages in this range. this
  584. * may block.
  585. */
  586. truncate_inode_pages_range(inode->i_mapping, pos, pos+len);
  587. } else {
  588. pages = alloc_page_vector(num_pages);
  589. if (IS_ERR(pages)) {
  590. ret = PTR_ERR(pages);
  591. goto out;
  592. }
  593. ret = copy_user_to_page_vector(pages, data, pos, len);
  594. if (ret < 0) {
  595. ceph_release_page_vector(pages, num_pages);
  596. goto out;
  597. }
  598. if ((file->f_flags & O_SYNC) == 0) {
  599. /* get a second commit callback */
  600. req->r_safe_callback = sync_write_commit;
  601. req->r_own_pages = 1;
  602. }
  603. }
  604. req->r_pages = pages;
  605. req->r_num_pages = num_pages;
  606. req->r_inode = inode;
  607. ret = ceph_osdc_start_request(&client->osdc, req, false);
  608. if (!ret) {
  609. if (req->r_safe_callback) {
  610. /*
  611. * Add to inode unsafe list only after we
  612. * start_request so that a tid has been assigned.
  613. */
  614. spin_lock(&ci->i_unsafe_lock);
  615. list_add(&ci->i_unsafe_writes, &req->r_unsafe_item);
  616. spin_unlock(&ci->i_unsafe_lock);
  617. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  618. }
  619. ret = ceph_osdc_wait_request(&client->osdc, req);
  620. }
  621. if (file->f_flags & O_DIRECT)
  622. put_page_vector(pages, num_pages);
  623. else if (file->f_flags & O_SYNC)
  624. ceph_release_page_vector(pages, num_pages);
  625. out:
  626. ceph_osdc_put_request(req);
  627. if (ret == 0) {
  628. pos += len;
  629. written += len;
  630. left -= len;
  631. if (left)
  632. goto more;
  633. ret = written;
  634. *offset = pos;
  635. if (pos > i_size_read(inode))
  636. check_caps = ceph_inode_set_size(inode, pos);
  637. if (check_caps)
  638. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY,
  639. NULL);
  640. }
  641. return ret;
  642. }
  643. /*
  644. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  645. * Atomically grab references, so that those bits are not released
  646. * back to the MDS mid-read.
  647. *
  648. * Hmm, the sync read case isn't actually async... should it be?
  649. */
  650. static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov,
  651. unsigned long nr_segs, loff_t pos)
  652. {
  653. struct file *filp = iocb->ki_filp;
  654. loff_t *ppos = &iocb->ki_pos;
  655. size_t len = iov->iov_len;
  656. struct inode *inode = filp->f_dentry->d_inode;
  657. struct ceph_inode_info *ci = ceph_inode(inode);
  658. void *base = iov->iov_base;
  659. ssize_t ret;
  660. int got = 0;
  661. int checkeof = 0, read = 0;
  662. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  663. inode, ceph_vinop(inode), pos, (unsigned)len, inode);
  664. again:
  665. __ceph_do_pending_vmtruncate(inode);
  666. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, CEPH_CAP_FILE_CACHE,
  667. &got, -1);
  668. if (ret < 0)
  669. goto out;
  670. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  671. inode, ceph_vinop(inode), pos, (unsigned)len,
  672. ceph_cap_string(got));
  673. if ((got & CEPH_CAP_FILE_CACHE) == 0 ||
  674. (iocb->ki_filp->f_flags & O_DIRECT) ||
  675. (inode->i_sb->s_flags & MS_SYNCHRONOUS))
  676. /* hmm, this isn't really async... */
  677. ret = ceph_sync_read(filp, base, len, ppos, &checkeof);
  678. else
  679. ret = generic_file_aio_read(iocb, iov, nr_segs, pos);
  680. out:
  681. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  682. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  683. ceph_put_cap_refs(ci, got);
  684. if (checkeof && ret >= 0) {
  685. int statret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  686. /* hit EOF or hole? */
  687. if (statret == 0 && *ppos < inode->i_size) {
  688. dout("aio_read sync_read hit hole, reading more\n");
  689. read += ret;
  690. base += ret;
  691. len -= ret;
  692. checkeof = 0;
  693. goto again;
  694. }
  695. }
  696. if (ret >= 0)
  697. ret += read;
  698. return ret;
  699. }
  700. /*
  701. * Take cap references to avoid releasing caps to MDS mid-write.
  702. *
  703. * If we are synchronous, and write with an old snap context, the OSD
  704. * may return EOLDSNAPC. In that case, retry the write.. _after_
  705. * dropping our cap refs and allowing the pending snap to logically
  706. * complete _before_ this write occurs.
  707. *
  708. * If we are near ENOSPC, write synchronously.
  709. */
  710. static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
  711. unsigned long nr_segs, loff_t pos)
  712. {
  713. struct file *file = iocb->ki_filp;
  714. struct inode *inode = file->f_dentry->d_inode;
  715. struct ceph_inode_info *ci = ceph_inode(inode);
  716. struct ceph_osd_client *osdc = &ceph_client(inode->i_sb)->osdc;
  717. loff_t endoff = pos + iov->iov_len;
  718. int got = 0;
  719. int ret;
  720. if (ceph_snap(inode) != CEPH_NOSNAP)
  721. return -EROFS;
  722. retry_snap:
  723. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  724. return -ENOSPC;
  725. __ceph_do_pending_vmtruncate(inode);
  726. dout("aio_write %p %llx.%llx %llu~%u getting caps. i_size %llu\n",
  727. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  728. inode->i_size);
  729. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER,
  730. &got, endoff);
  731. if (ret < 0)
  732. goto out;
  733. dout("aio_write %p %llx.%llx %llu~%u got cap refs on %s\n",
  734. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  735. ceph_cap_string(got));
  736. if ((got & CEPH_CAP_FILE_BUFFER) == 0 ||
  737. (iocb->ki_filp->f_flags & O_DIRECT) ||
  738. (inode->i_sb->s_flags & MS_SYNCHRONOUS)) {
  739. ret = ceph_sync_write(file, iov->iov_base, iov->iov_len,
  740. &iocb->ki_pos);
  741. } else {
  742. ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
  743. if ((ret >= 0 || ret == -EIOCBQUEUED) &&
  744. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host)
  745. || ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL)))
  746. ret = vfs_fsync_range(file, file->f_path.dentry,
  747. pos, pos + ret - 1, 1);
  748. }
  749. if (ret >= 0) {
  750. spin_lock(&inode->i_lock);
  751. __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  752. spin_unlock(&inode->i_lock);
  753. }
  754. out:
  755. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  756. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  757. ceph_cap_string(got));
  758. ceph_put_cap_refs(ci, got);
  759. if (ret == -EOLDSNAPC) {
  760. dout("aio_write %p %llx.%llx %llu~%u got EOLDSNAPC, retrying\n",
  761. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len);
  762. goto retry_snap;
  763. }
  764. return ret;
  765. }
  766. /*
  767. * llseek. be sure to verify file size on SEEK_END.
  768. */
  769. static loff_t ceph_llseek(struct file *file, loff_t offset, int origin)
  770. {
  771. struct inode *inode = file->f_mapping->host;
  772. int ret;
  773. mutex_lock(&inode->i_mutex);
  774. __ceph_do_pending_vmtruncate(inode);
  775. switch (origin) {
  776. case SEEK_END:
  777. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  778. if (ret < 0) {
  779. offset = ret;
  780. goto out;
  781. }
  782. offset += inode->i_size;
  783. break;
  784. case SEEK_CUR:
  785. /*
  786. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  787. * position-querying operation. Avoid rewriting the "same"
  788. * f_pos value back to the file because a concurrent read(),
  789. * write() or lseek() might have altered it
  790. */
  791. if (offset == 0) {
  792. offset = file->f_pos;
  793. goto out;
  794. }
  795. offset += file->f_pos;
  796. break;
  797. }
  798. if (offset < 0 || offset > inode->i_sb->s_maxbytes) {
  799. offset = -EINVAL;
  800. goto out;
  801. }
  802. /* Special lock needed here? */
  803. if (offset != file->f_pos) {
  804. file->f_pos = offset;
  805. file->f_version = 0;
  806. }
  807. out:
  808. mutex_unlock(&inode->i_mutex);
  809. return offset;
  810. }
  811. const struct file_operations ceph_file_fops = {
  812. .open = ceph_open,
  813. .release = ceph_release,
  814. .llseek = ceph_llseek,
  815. .read = do_sync_read,
  816. .write = do_sync_write,
  817. .aio_read = ceph_aio_read,
  818. .aio_write = ceph_aio_write,
  819. .mmap = ceph_mmap,
  820. .fsync = ceph_fsync,
  821. .splice_read = generic_file_splice_read,
  822. .splice_write = generic_file_splice_write,
  823. .unlocked_ioctl = ceph_ioctl,
  824. .compat_ioctl = ceph_ioctl,
  825. };