file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. {
  387. struct ceph_client *client = ceph_inode_to_client(inode);
  388. struct ceph_inode_info *ci = ceph_inode(inode);
  389. u64 pos, this_len;
  390. int page_off = off & ~PAGE_CACHE_MASK; /* first byte's offset in page */
  391. int left, pages_left;
  392. int read;
  393. struct page **page_pos;
  394. int ret;
  395. bool hit_stripe, was_short;
  396. /*
  397. * we may need to do multiple reads. not atomic, unfortunately.
  398. */
  399. pos = off;
  400. left = len;
  401. page_pos = pages;
  402. pages_left = num_pages;
  403. read = 0;
  404. more:
  405. this_len = left;
  406. ret = ceph_osdc_readpages(&client->osdc, ceph_vino(inode),
  407. &ci->i_layout, pos, &this_len,
  408. ci->i_truncate_seq,
  409. ci->i_truncate_size,
  410. page_pos, pages_left);
  411. hit_stripe = this_len < left;
  412. was_short = ret >= 0 && ret < this_len;
  413. if (ret == -ENOENT)
  414. ret = 0;
  415. dout("striped_read %llu~%u (read %u) got %d%s%s\n", pos, left, read,
  416. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  417. if (ret > 0) {
  418. int didpages =
  419. ((pos & ~PAGE_CACHE_MASK) + ret) >> PAGE_CACHE_SHIFT;
  420. if (read < pos - off) {
  421. dout(" zero gap %llu to %llu\n", off + read, pos);
  422. zero_page_vector_range(page_off + read,
  423. pos - off - read, pages);
  424. }
  425. pos += ret;
  426. read = pos - off;
  427. left -= ret;
  428. page_pos += didpages;
  429. pages_left -= didpages;
  430. /* hit stripe? */
  431. if (left && hit_stripe)
  432. goto more;
  433. }
  434. if (was_short) {
  435. /* was original extent fully inside i_size? */
  436. if (pos + left <= inode->i_size) {
  437. dout("zero tail\n");
  438. zero_page_vector_range(page_off + read, len - read,
  439. pages);
  440. read = len;
  441. goto out;
  442. }
  443. /* check i_size */
  444. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  445. if (ret < 0)
  446. goto out;
  447. /* hit EOF? */
  448. if (pos >= inode->i_size)
  449. goto out;
  450. goto more;
  451. }
  452. out:
  453. if (ret >= 0)
  454. ret = read;
  455. dout("striped_read returns %d\n", ret);
  456. return ret;
  457. }
  458. /*
  459. * Completely synchronous read and write methods. Direct from __user
  460. * buffer to osd, or directly to user pages (if O_DIRECT).
  461. *
  462. * If the read spans object boundary, just do multiple reads.
  463. */
  464. static ssize_t ceph_sync_read(struct file *file, char __user *data,
  465. unsigned len, loff_t *poff)
  466. {
  467. struct inode *inode = file->f_dentry->d_inode;
  468. struct page **pages;
  469. u64 off = *poff;
  470. int num_pages = calc_pages_for(off, len);
  471. int ret;
  472. dout("sync_read on file %p %llu~%u %s\n", file, off, len,
  473. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  474. if (file->f_flags & O_DIRECT) {
  475. pages = get_direct_page_vector(data, num_pages, off, len);
  476. /*
  477. * flush any page cache pages in this range. this
  478. * will make concurrent normal and O_DIRECT io slow,
  479. * but it will at least behave sensibly when they are
  480. * in sequence.
  481. */
  482. } else {
  483. pages = alloc_page_vector(num_pages);
  484. }
  485. if (IS_ERR(pages))
  486. return PTR_ERR(pages);
  487. ret = filemap_write_and_wait(inode->i_mapping);
  488. if (ret < 0)
  489. goto done;
  490. ret = striped_read(inode, off, len, pages, num_pages);
  491. if (ret >= 0 && (file->f_flags & O_DIRECT) == 0)
  492. ret = copy_page_vector_to_user(pages, data, off, ret);
  493. if (ret >= 0)
  494. *poff = off + ret;
  495. done:
  496. if (file->f_flags & O_DIRECT)
  497. put_page_vector(pages, num_pages);
  498. else
  499. ceph_release_page_vector(pages, num_pages);
  500. dout("sync_read result %d\n", ret);
  501. return ret;
  502. }
  503. /*
  504. * Write commit callback, called if we requested both an ACK and
  505. * ONDISK commit reply from the OSD.
  506. */
  507. static void sync_write_commit(struct ceph_osd_request *req,
  508. struct ceph_msg *msg)
  509. {
  510. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  511. dout("sync_write_commit %p tid %llu\n", req, req->r_tid);
  512. spin_lock(&ci->i_unsafe_lock);
  513. list_del_init(&req->r_unsafe_item);
  514. spin_unlock(&ci->i_unsafe_lock);
  515. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  516. }
  517. /*
  518. * Synchronous write, straight from __user pointer or user pages (if
  519. * O_DIRECT).
  520. *
  521. * If write spans object boundary, just do multiple writes. (For a
  522. * correct atomic write, we should e.g. take write locks on all
  523. * objects, rollback on failure, etc.)
  524. */
  525. static ssize_t ceph_sync_write(struct file *file, const char __user *data,
  526. size_t left, loff_t *offset)
  527. {
  528. struct inode *inode = file->f_dentry->d_inode;
  529. struct ceph_inode_info *ci = ceph_inode(inode);
  530. struct ceph_client *client = ceph_inode_to_client(inode);
  531. struct ceph_osd_request *req;
  532. struct page **pages;
  533. int num_pages;
  534. long long unsigned pos;
  535. u64 len;
  536. int written = 0;
  537. int flags;
  538. int do_sync = 0;
  539. int check_caps = 0;
  540. int ret;
  541. struct timespec mtime = CURRENT_TIME;
  542. if (ceph_snap(file->f_dentry->d_inode) != CEPH_NOSNAP)
  543. return -EROFS;
  544. dout("sync_write on file %p %lld~%u %s\n", file, *offset,
  545. (unsigned)left, (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  546. if (file->f_flags & O_APPEND)
  547. pos = i_size_read(inode);
  548. else
  549. pos = *offset;
  550. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + left);
  551. if (ret < 0)
  552. return ret;
  553. ret = invalidate_inode_pages2_range(inode->i_mapping,
  554. pos >> PAGE_CACHE_SHIFT,
  555. (pos + left) >> PAGE_CACHE_SHIFT);
  556. if (ret < 0)
  557. dout("invalidate_inode_pages2_range returned %d\n", ret);
  558. flags = CEPH_OSD_FLAG_ORDERSNAP |
  559. CEPH_OSD_FLAG_ONDISK |
  560. CEPH_OSD_FLAG_WRITE;
  561. if ((file->f_flags & (O_SYNC|O_DIRECT)) == 0)
  562. flags |= CEPH_OSD_FLAG_ACK;
  563. else
  564. do_sync = 1;
  565. /*
  566. * we may need to do multiple writes here if we span an object
  567. * boundary. this isn't atomic, unfortunately. :(
  568. */
  569. more:
  570. len = left;
  571. req = ceph_osdc_new_request(&client->osdc, &ci->i_layout,
  572. ceph_vino(inode), pos, &len,
  573. CEPH_OSD_OP_WRITE, flags,
  574. ci->i_snap_realm->cached_context,
  575. do_sync,
  576. ci->i_truncate_seq, ci->i_truncate_size,
  577. &mtime, false, 2);
  578. if (IS_ERR(req))
  579. return PTR_ERR(req);
  580. num_pages = calc_pages_for(pos, len);
  581. if (file->f_flags & O_DIRECT) {
  582. pages = get_direct_page_vector(data, num_pages, pos, len);
  583. if (IS_ERR(pages)) {
  584. ret = PTR_ERR(pages);
  585. goto out;
  586. }
  587. /*
  588. * throw out any page cache pages in this range. this
  589. * may block.
  590. */
  591. truncate_inode_pages_range(inode->i_mapping, pos, pos+len);
  592. } else {
  593. pages = alloc_page_vector(num_pages);
  594. if (IS_ERR(pages)) {
  595. ret = PTR_ERR(pages);
  596. goto out;
  597. }
  598. ret = copy_user_to_page_vector(pages, data, pos, len);
  599. if (ret < 0) {
  600. ceph_release_page_vector(pages, num_pages);
  601. goto out;
  602. }
  603. if ((file->f_flags & O_SYNC) == 0) {
  604. /* get a second commit callback */
  605. req->r_safe_callback = sync_write_commit;
  606. req->r_own_pages = 1;
  607. }
  608. }
  609. req->r_pages = pages;
  610. req->r_num_pages = num_pages;
  611. req->r_inode = inode;
  612. ret = ceph_osdc_start_request(&client->osdc, req, false);
  613. if (!ret) {
  614. if (req->r_safe_callback) {
  615. /*
  616. * Add to inode unsafe list only after we
  617. * start_request so that a tid has been assigned.
  618. */
  619. spin_lock(&ci->i_unsafe_lock);
  620. list_add(&ci->i_unsafe_writes, &req->r_unsafe_item);
  621. spin_unlock(&ci->i_unsafe_lock);
  622. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  623. }
  624. ret = ceph_osdc_wait_request(&client->osdc, req);
  625. }
  626. if (file->f_flags & O_DIRECT)
  627. put_page_vector(pages, num_pages);
  628. else if (file->f_flags & O_SYNC)
  629. ceph_release_page_vector(pages, num_pages);
  630. out:
  631. ceph_osdc_put_request(req);
  632. if (ret == 0) {
  633. pos += len;
  634. written += len;
  635. left -= len;
  636. if (left)
  637. goto more;
  638. ret = written;
  639. *offset = pos;
  640. if (pos > i_size_read(inode))
  641. check_caps = ceph_inode_set_size(inode, pos);
  642. if (check_caps)
  643. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY,
  644. NULL);
  645. }
  646. return ret;
  647. }
  648. /*
  649. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  650. * Atomically grab references, so that those bits are not released
  651. * back to the MDS mid-read.
  652. *
  653. * Hmm, the sync read case isn't actually async... should it be?
  654. */
  655. static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov,
  656. unsigned long nr_segs, loff_t pos)
  657. {
  658. struct file *filp = iocb->ki_filp;
  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. ssize_t ret;
  664. int got = 0;
  665. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  666. inode, ceph_vinop(inode), pos, (unsigned)len, inode);
  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, iov->iov_base, len, ppos);
  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. return ret;
  687. }
  688. /*
  689. * Take cap references to avoid releasing caps to MDS mid-write.
  690. *
  691. * If we are synchronous, and write with an old snap context, the OSD
  692. * may return EOLDSNAPC. In that case, retry the write.. _after_
  693. * dropping our cap refs and allowing the pending snap to logically
  694. * complete _before_ this write occurs.
  695. *
  696. * If we are near ENOSPC, write synchronously.
  697. */
  698. static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
  699. unsigned long nr_segs, loff_t pos)
  700. {
  701. struct file *file = iocb->ki_filp;
  702. struct inode *inode = file->f_dentry->d_inode;
  703. struct ceph_inode_info *ci = ceph_inode(inode);
  704. struct ceph_osd_client *osdc = &ceph_client(inode->i_sb)->osdc;
  705. loff_t endoff = pos + iov->iov_len;
  706. int got = 0;
  707. int ret;
  708. if (ceph_snap(inode) != CEPH_NOSNAP)
  709. return -EROFS;
  710. retry_snap:
  711. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  712. return -ENOSPC;
  713. __ceph_do_pending_vmtruncate(inode);
  714. dout("aio_write %p %llx.%llx %llu~%u getting caps. i_size %llu\n",
  715. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  716. inode->i_size);
  717. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER,
  718. &got, endoff);
  719. if (ret < 0)
  720. goto out;
  721. dout("aio_write %p %llx.%llx %llu~%u got cap refs on %s\n",
  722. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  723. ceph_cap_string(got));
  724. if ((got & CEPH_CAP_FILE_BUFFER) == 0 ||
  725. (iocb->ki_filp->f_flags & O_DIRECT) ||
  726. (inode->i_sb->s_flags & MS_SYNCHRONOUS)) {
  727. ret = ceph_sync_write(file, iov->iov_base, iov->iov_len,
  728. &iocb->ki_pos);
  729. } else {
  730. ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
  731. if ((ret >= 0 || ret == -EIOCBQUEUED) &&
  732. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host)
  733. || ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL)))
  734. ret = vfs_fsync_range(file, file->f_path.dentry,
  735. pos, pos + ret - 1, 1);
  736. }
  737. if (ret >= 0) {
  738. spin_lock(&inode->i_lock);
  739. __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  740. spin_unlock(&inode->i_lock);
  741. }
  742. out:
  743. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  744. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  745. ceph_cap_string(got));
  746. ceph_put_cap_refs(ci, got);
  747. if (ret == -EOLDSNAPC) {
  748. dout("aio_write %p %llx.%llx %llu~%u got EOLDSNAPC, retrying\n",
  749. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len);
  750. goto retry_snap;
  751. }
  752. return ret;
  753. }
  754. /*
  755. * llseek. be sure to verify file size on SEEK_END.
  756. */
  757. static loff_t ceph_llseek(struct file *file, loff_t offset, int origin)
  758. {
  759. struct inode *inode = file->f_mapping->host;
  760. int ret;
  761. mutex_lock(&inode->i_mutex);
  762. __ceph_do_pending_vmtruncate(inode);
  763. switch (origin) {
  764. case SEEK_END:
  765. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  766. if (ret < 0) {
  767. offset = ret;
  768. goto out;
  769. }
  770. offset += inode->i_size;
  771. break;
  772. case SEEK_CUR:
  773. /*
  774. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  775. * position-querying operation. Avoid rewriting the "same"
  776. * f_pos value back to the file because a concurrent read(),
  777. * write() or lseek() might have altered it
  778. */
  779. if (offset == 0) {
  780. offset = file->f_pos;
  781. goto out;
  782. }
  783. offset += file->f_pos;
  784. break;
  785. }
  786. if (offset < 0 || offset > inode->i_sb->s_maxbytes) {
  787. offset = -EINVAL;
  788. goto out;
  789. }
  790. /* Special lock needed here? */
  791. if (offset != file->f_pos) {
  792. file->f_pos = offset;
  793. file->f_version = 0;
  794. }
  795. out:
  796. mutex_unlock(&inode->i_mutex);
  797. return offset;
  798. }
  799. const struct file_operations ceph_file_fops = {
  800. .open = ceph_open,
  801. .release = ceph_release,
  802. .llseek = ceph_llseek,
  803. .read = do_sync_read,
  804. .write = do_sync_write,
  805. .aio_read = ceph_aio_read,
  806. .aio_write = ceph_aio_write,
  807. .mmap = ceph_mmap,
  808. .fsync = ceph_fsync,
  809. .splice_read = generic_file_splice_read,
  810. .splice_write = generic_file_splice_write,
  811. .unlocked_ioctl = ceph_ioctl,
  812. .compat_ioctl = ceph_ioctl,
  813. };