file.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/sched.h>
  4. #include <linux/slab.h>
  5. #include <linux/file.h>
  6. #include <linux/mount.h>
  7. #include <linux/namei.h>
  8. #include <linux/writeback.h>
  9. #include <linux/aio.h>
  10. #include <linux/falloc.h>
  11. #include "super.h"
  12. #include "mds_client.h"
  13. /*
  14. * Ceph file operations
  15. *
  16. * Implement basic open/close functionality, and implement
  17. * read/write.
  18. *
  19. * We implement three modes of file I/O:
  20. * - buffered uses the generic_file_aio_{read,write} helpers
  21. *
  22. * - synchronous is used when there is multi-client read/write
  23. * sharing, avoids the page cache, and synchronously waits for an
  24. * ack from the OSD.
  25. *
  26. * - direct io takes the variant of the sync path that references
  27. * user pages directly.
  28. *
  29. * fsync() flushes and waits on dirty pages, but just queues metadata
  30. * for writeback: since the MDS can recover size and mtime there is no
  31. * need to wait for MDS acknowledgement.
  32. */
  33. /*
  34. * Prepare an open request. Preallocate ceph_cap to avoid an
  35. * inopportune ENOMEM later.
  36. */
  37. static struct ceph_mds_request *
  38. prepare_open_request(struct super_block *sb, int flags, int create_mode)
  39. {
  40. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  41. struct ceph_mds_client *mdsc = fsc->mdsc;
  42. struct ceph_mds_request *req;
  43. int want_auth = USE_ANY_MDS;
  44. int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
  45. if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
  46. want_auth = USE_AUTH_MDS;
  47. req = ceph_mdsc_create_request(mdsc, op, want_auth);
  48. if (IS_ERR(req))
  49. goto out;
  50. req->r_fmode = ceph_flags_to_mode(flags);
  51. req->r_args.open.flags = cpu_to_le32(flags);
  52. req->r_args.open.mode = cpu_to_le32(create_mode);
  53. out:
  54. return req;
  55. }
  56. /*
  57. * initialize private struct file data.
  58. * if we fail, clean up by dropping fmode reference on the ceph_inode
  59. */
  60. static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
  61. {
  62. struct ceph_file_info *cf;
  63. int ret = 0;
  64. switch (inode->i_mode & S_IFMT) {
  65. case S_IFREG:
  66. case S_IFDIR:
  67. dout("init_file %p %p 0%o (regular)\n", inode, file,
  68. inode->i_mode);
  69. cf = kmem_cache_alloc(ceph_file_cachep, GFP_NOFS | __GFP_ZERO);
  70. if (cf == NULL) {
  71. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  72. return -ENOMEM;
  73. }
  74. cf->fmode = fmode;
  75. cf->next_offset = 2;
  76. file->private_data = cf;
  77. BUG_ON(inode->i_fop->release != ceph_release);
  78. break;
  79. case S_IFLNK:
  80. dout("init_file %p %p 0%o (symlink)\n", inode, file,
  81. inode->i_mode);
  82. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  83. break;
  84. default:
  85. dout("init_file %p %p 0%o (special)\n", inode, file,
  86. inode->i_mode);
  87. /*
  88. * we need to drop the open ref now, since we don't
  89. * have .release set to ceph_release.
  90. */
  91. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  92. BUG_ON(inode->i_fop->release == ceph_release);
  93. /* call the proper open fop */
  94. ret = inode->i_fop->open(inode, file);
  95. }
  96. return ret;
  97. }
  98. /*
  99. * If we already have the requisite capabilities, we can satisfy
  100. * the open request locally (no need to request new caps from the
  101. * MDS). We do, however, need to inform the MDS (asynchronously)
  102. * if our wanted caps set expands.
  103. */
  104. int ceph_open(struct inode *inode, struct file *file)
  105. {
  106. struct ceph_inode_info *ci = ceph_inode(inode);
  107. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  108. struct ceph_mds_client *mdsc = fsc->mdsc;
  109. struct ceph_mds_request *req;
  110. struct ceph_file_info *cf = file->private_data;
  111. struct inode *parent_inode = NULL;
  112. int err;
  113. int flags, fmode, wanted;
  114. if (cf) {
  115. dout("open file %p is already opened\n", file);
  116. return 0;
  117. }
  118. /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
  119. flags = file->f_flags & ~(O_CREAT|O_EXCL);
  120. if (S_ISDIR(inode->i_mode))
  121. flags = O_DIRECTORY; /* mds likes to know */
  122. dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
  123. ceph_vinop(inode), file, flags, file->f_flags);
  124. fmode = ceph_flags_to_mode(flags);
  125. wanted = ceph_caps_for_mode(fmode);
  126. /* snapped files are read-only */
  127. if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
  128. return -EROFS;
  129. /* trivially open snapdir */
  130. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  131. spin_lock(&ci->i_ceph_lock);
  132. __ceph_get_fmode(ci, fmode);
  133. spin_unlock(&ci->i_ceph_lock);
  134. return ceph_init_file(inode, file, fmode);
  135. }
  136. /*
  137. * No need to block if we have caps on the auth MDS (for
  138. * write) or any MDS (for read). Update wanted set
  139. * asynchronously.
  140. */
  141. spin_lock(&ci->i_ceph_lock);
  142. if (__ceph_is_any_real_caps(ci) &&
  143. (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
  144. int mds_wanted = __ceph_caps_mds_wanted(ci);
  145. int issued = __ceph_caps_issued(ci, NULL);
  146. dout("open %p fmode %d want %s issued %s using existing\n",
  147. inode, fmode, ceph_cap_string(wanted),
  148. ceph_cap_string(issued));
  149. __ceph_get_fmode(ci, fmode);
  150. spin_unlock(&ci->i_ceph_lock);
  151. /* adjust wanted? */
  152. if ((issued & wanted) != wanted &&
  153. (mds_wanted & wanted) != wanted &&
  154. ceph_snap(inode) != CEPH_SNAPDIR)
  155. ceph_check_caps(ci, 0, NULL);
  156. return ceph_init_file(inode, file, fmode);
  157. } else if (ceph_snap(inode) != CEPH_NOSNAP &&
  158. (ci->i_snap_caps & wanted) == wanted) {
  159. __ceph_get_fmode(ci, fmode);
  160. spin_unlock(&ci->i_ceph_lock);
  161. return ceph_init_file(inode, file, fmode);
  162. }
  163. spin_unlock(&ci->i_ceph_lock);
  164. dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
  165. req = prepare_open_request(inode->i_sb, flags, 0);
  166. if (IS_ERR(req)) {
  167. err = PTR_ERR(req);
  168. goto out;
  169. }
  170. req->r_inode = inode;
  171. ihold(inode);
  172. req->r_num_caps = 1;
  173. if (flags & (O_CREAT|O_TRUNC))
  174. parent_inode = ceph_get_dentry_parent_inode(file->f_dentry);
  175. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  176. iput(parent_inode);
  177. if (!err)
  178. err = ceph_init_file(inode, file, req->r_fmode);
  179. ceph_mdsc_put_request(req);
  180. dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
  181. out:
  182. return err;
  183. }
  184. /*
  185. * Do a lookup + open with a single request. If we get a non-existent
  186. * file or symlink, return 1 so the VFS can retry.
  187. */
  188. int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
  189. struct file *file, unsigned flags, umode_t mode,
  190. int *opened)
  191. {
  192. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  193. struct ceph_mds_client *mdsc = fsc->mdsc;
  194. struct ceph_mds_request *req;
  195. struct dentry *dn;
  196. int err;
  197. dout("atomic_open %p dentry %p '%.*s' %s flags %d mode 0%o\n",
  198. dir, dentry, dentry->d_name.len, dentry->d_name.name,
  199. d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
  200. if (dentry->d_name.len > NAME_MAX)
  201. return -ENAMETOOLONG;
  202. err = ceph_init_dentry(dentry);
  203. if (err < 0)
  204. return err;
  205. /* do the open */
  206. req = prepare_open_request(dir->i_sb, flags, mode);
  207. if (IS_ERR(req))
  208. return PTR_ERR(req);
  209. req->r_dentry = dget(dentry);
  210. req->r_num_caps = 2;
  211. if (flags & O_CREAT) {
  212. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  213. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  214. }
  215. req->r_locked_dir = dir; /* caller holds dir->i_mutex */
  216. err = ceph_mdsc_do_request(mdsc,
  217. (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
  218. req);
  219. if (err)
  220. goto out_err;
  221. err = ceph_handle_snapdir(req, dentry, err);
  222. if (err == 0 && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  223. err = ceph_handle_notrace_create(dir, dentry);
  224. if (d_unhashed(dentry)) {
  225. dn = ceph_finish_lookup(req, dentry, err);
  226. if (IS_ERR(dn))
  227. err = PTR_ERR(dn);
  228. } else {
  229. /* we were given a hashed negative dentry */
  230. dn = NULL;
  231. }
  232. if (err)
  233. goto out_err;
  234. if (dn || dentry->d_inode == NULL || S_ISLNK(dentry->d_inode->i_mode)) {
  235. /* make vfs retry on splice, ENOENT, or symlink */
  236. dout("atomic_open finish_no_open on dn %p\n", dn);
  237. err = finish_no_open(file, dn);
  238. } else {
  239. dout("atomic_open finish_open on dn %p\n", dn);
  240. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  241. *opened |= FILE_CREATED;
  242. }
  243. err = finish_open(file, dentry, ceph_open, opened);
  244. }
  245. out_err:
  246. ceph_mdsc_put_request(req);
  247. dout("atomic_open result=%d\n", err);
  248. return err;
  249. }
  250. int ceph_release(struct inode *inode, struct file *file)
  251. {
  252. struct ceph_inode_info *ci = ceph_inode(inode);
  253. struct ceph_file_info *cf = file->private_data;
  254. dout("release inode %p file %p\n", inode, file);
  255. ceph_put_fmode(ci, cf->fmode);
  256. if (cf->last_readdir)
  257. ceph_mdsc_put_request(cf->last_readdir);
  258. kfree(cf->last_name);
  259. kfree(cf->dir_info);
  260. dput(cf->dentry);
  261. kmem_cache_free(ceph_file_cachep, cf);
  262. /* wake up anyone waiting for caps on this inode */
  263. wake_up_all(&ci->i_cap_wq);
  264. return 0;
  265. }
  266. /*
  267. * Read a range of bytes striped over one or more objects. Iterate over
  268. * objects we stripe over. (That's not atomic, but good enough for now.)
  269. *
  270. * If we get a short result from the OSD, check against i_size; we need to
  271. * only return a short read to the caller if we hit EOF.
  272. */
  273. static int striped_read(struct inode *inode,
  274. u64 off, u64 len,
  275. struct page **pages, int num_pages,
  276. int *checkeof, bool o_direct,
  277. unsigned long buf_align)
  278. {
  279. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  280. struct ceph_inode_info *ci = ceph_inode(inode);
  281. u64 pos, this_len, left;
  282. int io_align, page_align;
  283. int pages_left;
  284. int read;
  285. struct page **page_pos;
  286. int ret;
  287. bool hit_stripe, was_short;
  288. /*
  289. * we may need to do multiple reads. not atomic, unfortunately.
  290. */
  291. pos = off;
  292. left = len;
  293. page_pos = pages;
  294. pages_left = num_pages;
  295. read = 0;
  296. io_align = off & ~PAGE_MASK;
  297. more:
  298. if (o_direct)
  299. page_align = (pos - io_align + buf_align) & ~PAGE_MASK;
  300. else
  301. page_align = pos & ~PAGE_MASK;
  302. this_len = left;
  303. ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
  304. &ci->i_layout, pos, &this_len,
  305. ci->i_truncate_seq,
  306. ci->i_truncate_size,
  307. page_pos, pages_left, page_align);
  308. if (ret == -ENOENT)
  309. ret = 0;
  310. hit_stripe = this_len < left;
  311. was_short = ret >= 0 && ret < this_len;
  312. dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, left, read,
  313. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  314. if (ret >= 0) {
  315. int didpages;
  316. if (was_short && (pos + ret < inode->i_size)) {
  317. u64 tmp = min(this_len - ret,
  318. inode->i_size - pos - ret);
  319. dout(" zero gap %llu to %llu\n",
  320. pos + ret, pos + ret + tmp);
  321. ceph_zero_page_vector_range(page_align + read + ret,
  322. tmp, pages);
  323. ret += tmp;
  324. }
  325. didpages = (page_align + ret) >> PAGE_CACHE_SHIFT;
  326. pos += ret;
  327. read = pos - off;
  328. left -= ret;
  329. page_pos += didpages;
  330. pages_left -= didpages;
  331. /* hit stripe and need continue*/
  332. if (left && hit_stripe && pos < inode->i_size)
  333. goto more;
  334. }
  335. if (ret >= 0) {
  336. ret = read;
  337. /* did we bounce off eof? */
  338. if (pos + left > inode->i_size)
  339. *checkeof = 1;
  340. }
  341. dout("striped_read returns %d\n", ret);
  342. return ret;
  343. }
  344. /*
  345. * Completely synchronous read and write methods. Direct from __user
  346. * buffer to osd, or directly to user pages (if O_DIRECT).
  347. *
  348. * If the read spans object boundary, just do multiple reads.
  349. */
  350. static ssize_t ceph_sync_read(struct file *file, char __user *data,
  351. unsigned len, loff_t *poff, int *checkeof)
  352. {
  353. struct inode *inode = file_inode(file);
  354. struct page **pages;
  355. u64 off = *poff;
  356. int num_pages, ret;
  357. dout("sync_read on file %p %llu~%u %s\n", file, off, len,
  358. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  359. if (file->f_flags & O_DIRECT) {
  360. num_pages = calc_pages_for((unsigned long)data, len);
  361. pages = ceph_get_direct_page_vector(data, num_pages, true);
  362. } else {
  363. num_pages = calc_pages_for(off, len);
  364. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  365. }
  366. if (IS_ERR(pages))
  367. return PTR_ERR(pages);
  368. /*
  369. * flush any page cache pages in this range. this
  370. * will make concurrent normal and sync io slow,
  371. * but it will at least behave sensibly when they are
  372. * in sequence.
  373. */
  374. ret = filemap_write_and_wait(inode->i_mapping);
  375. if (ret < 0)
  376. goto done;
  377. ret = striped_read(inode, off, len, pages, num_pages, checkeof,
  378. file->f_flags & O_DIRECT,
  379. (unsigned long)data & ~PAGE_MASK);
  380. if (ret >= 0 && (file->f_flags & O_DIRECT) == 0)
  381. ret = ceph_copy_page_vector_to_user(pages, data, off, ret);
  382. if (ret >= 0)
  383. *poff = off + ret;
  384. done:
  385. if (file->f_flags & O_DIRECT)
  386. ceph_put_page_vector(pages, num_pages, true);
  387. else
  388. ceph_release_page_vector(pages, num_pages);
  389. dout("sync_read result %d\n", ret);
  390. return ret;
  391. }
  392. /*
  393. * Write commit request unsafe callback, called to tell us when a
  394. * request is unsafe (that is, in flight--has been handed to the
  395. * messenger to send to its target osd). It is called again when
  396. * we've received a response message indicating the request is
  397. * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
  398. * is completed early (and unsuccessfully) due to a timeout or
  399. * interrupt.
  400. *
  401. * This is used if we requested both an ACK and ONDISK commit reply
  402. * from the OSD.
  403. */
  404. static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
  405. {
  406. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  407. dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
  408. unsafe ? "un" : "");
  409. if (unsafe) {
  410. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  411. spin_lock(&ci->i_unsafe_lock);
  412. list_add_tail(&req->r_unsafe_item,
  413. &ci->i_unsafe_writes);
  414. spin_unlock(&ci->i_unsafe_lock);
  415. } else {
  416. spin_lock(&ci->i_unsafe_lock);
  417. list_del_init(&req->r_unsafe_item);
  418. spin_unlock(&ci->i_unsafe_lock);
  419. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  420. }
  421. }
  422. /*
  423. * Synchronous write, straight from __user pointer or user pages (if
  424. * O_DIRECT).
  425. *
  426. * If write spans object boundary, just do multiple writes. (For a
  427. * correct atomic write, we should e.g. take write locks on all
  428. * objects, rollback on failure, etc.)
  429. */
  430. static ssize_t ceph_sync_write(struct file *file, const char __user *data,
  431. size_t left, loff_t pos, loff_t *ppos)
  432. {
  433. struct inode *inode = file_inode(file);
  434. struct ceph_inode_info *ci = ceph_inode(inode);
  435. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  436. struct ceph_snap_context *snapc;
  437. struct ceph_vino vino;
  438. struct ceph_osd_request *req;
  439. int num_ops = 1;
  440. struct page **pages;
  441. int num_pages;
  442. u64 len;
  443. int written = 0;
  444. int flags;
  445. int check_caps = 0;
  446. int page_align, io_align;
  447. unsigned long buf_align;
  448. int ret;
  449. struct timespec mtime = CURRENT_TIME;
  450. bool own_pages = false;
  451. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  452. return -EROFS;
  453. dout("sync_write on file %p %lld~%u %s\n", file, pos,
  454. (unsigned)left, (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  455. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + left);
  456. if (ret < 0)
  457. return ret;
  458. ret = invalidate_inode_pages2_range(inode->i_mapping,
  459. pos >> PAGE_CACHE_SHIFT,
  460. (pos + left) >> PAGE_CACHE_SHIFT);
  461. if (ret < 0)
  462. dout("invalidate_inode_pages2_range returned %d\n", ret);
  463. flags = CEPH_OSD_FLAG_ORDERSNAP |
  464. CEPH_OSD_FLAG_ONDISK |
  465. CEPH_OSD_FLAG_WRITE;
  466. if ((file->f_flags & (O_SYNC|O_DIRECT)) == 0)
  467. flags |= CEPH_OSD_FLAG_ACK;
  468. else
  469. num_ops++; /* Also include a 'startsync' command. */
  470. /*
  471. * we may need to do multiple writes here if we span an object
  472. * boundary. this isn't atomic, unfortunately. :(
  473. */
  474. more:
  475. io_align = pos & ~PAGE_MASK;
  476. buf_align = (unsigned long)data & ~PAGE_MASK;
  477. len = left;
  478. snapc = ci->i_snap_realm->cached_context;
  479. vino = ceph_vino(inode);
  480. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  481. vino, pos, &len, num_ops,
  482. CEPH_OSD_OP_WRITE, flags, snapc,
  483. ci->i_truncate_seq, ci->i_truncate_size,
  484. false);
  485. if (IS_ERR(req))
  486. return PTR_ERR(req);
  487. /* write from beginning of first page, regardless of io alignment */
  488. page_align = file->f_flags & O_DIRECT ? buf_align : io_align;
  489. num_pages = calc_pages_for(page_align, len);
  490. if (file->f_flags & O_DIRECT) {
  491. pages = ceph_get_direct_page_vector(data, num_pages, false);
  492. if (IS_ERR(pages)) {
  493. ret = PTR_ERR(pages);
  494. goto out;
  495. }
  496. /*
  497. * throw out any page cache pages in this range. this
  498. * may block.
  499. */
  500. truncate_inode_pages_range(inode->i_mapping, pos,
  501. (pos+len) | (PAGE_CACHE_SIZE-1));
  502. } else {
  503. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  504. if (IS_ERR(pages)) {
  505. ret = PTR_ERR(pages);
  506. goto out;
  507. }
  508. ret = ceph_copy_user_to_page_vector(pages, data, pos, len);
  509. if (ret < 0) {
  510. ceph_release_page_vector(pages, num_pages);
  511. goto out;
  512. }
  513. if ((file->f_flags & O_SYNC) == 0) {
  514. /* get a second commit callback */
  515. req->r_unsafe_callback = ceph_sync_write_unsafe;
  516. req->r_inode = inode;
  517. own_pages = true;
  518. }
  519. }
  520. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
  521. false, own_pages);
  522. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  523. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  524. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  525. if (!ret)
  526. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  527. if (file->f_flags & O_DIRECT)
  528. ceph_put_page_vector(pages, num_pages, false);
  529. else if (file->f_flags & O_SYNC)
  530. ceph_release_page_vector(pages, num_pages);
  531. out:
  532. ceph_osdc_put_request(req);
  533. if (ret == 0) {
  534. pos += len;
  535. written += len;
  536. left -= len;
  537. data += len;
  538. if (left)
  539. goto more;
  540. ret = written;
  541. *ppos = pos;
  542. if (pos > i_size_read(inode))
  543. check_caps = ceph_inode_set_size(inode, pos);
  544. if (check_caps)
  545. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY,
  546. NULL);
  547. }
  548. return ret;
  549. }
  550. /*
  551. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  552. * Atomically grab references, so that those bits are not released
  553. * back to the MDS mid-read.
  554. *
  555. * Hmm, the sync read case isn't actually async... should it be?
  556. */
  557. static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov,
  558. unsigned long nr_segs, loff_t pos)
  559. {
  560. struct file *filp = iocb->ki_filp;
  561. struct ceph_file_info *fi = filp->private_data;
  562. loff_t *ppos = &iocb->ki_pos;
  563. size_t len = iov->iov_len;
  564. struct inode *inode = file_inode(filp);
  565. struct ceph_inode_info *ci = ceph_inode(inode);
  566. void __user *base = iov->iov_base;
  567. ssize_t ret;
  568. int want, got = 0;
  569. int checkeof = 0, read = 0;
  570. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  571. inode, ceph_vinop(inode), pos, (unsigned)len, inode);
  572. again:
  573. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  574. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  575. else
  576. want = CEPH_CAP_FILE_CACHE;
  577. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, &got, -1);
  578. if (ret < 0)
  579. goto out;
  580. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  581. inode, ceph_vinop(inode), pos, (unsigned)len,
  582. ceph_cap_string(got));
  583. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  584. (iocb->ki_filp->f_flags & O_DIRECT) ||
  585. (fi->flags & CEPH_F_SYNC))
  586. /* hmm, this isn't really async... */
  587. ret = ceph_sync_read(filp, base, len, ppos, &checkeof);
  588. else
  589. ret = generic_file_aio_read(iocb, iov, nr_segs, pos);
  590. out:
  591. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  592. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  593. ceph_put_cap_refs(ci, got);
  594. if (checkeof && ret >= 0) {
  595. int statret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  596. /* hit EOF or hole? */
  597. if (statret == 0 && *ppos < inode->i_size) {
  598. dout("aio_read sync_read hit hole, ppos %lld < size %lld, reading more\n", *ppos, inode->i_size);
  599. read += ret;
  600. base += ret;
  601. len -= ret;
  602. checkeof = 0;
  603. goto again;
  604. }
  605. }
  606. if (ret >= 0)
  607. ret += read;
  608. return ret;
  609. }
  610. /*
  611. * Take cap references to avoid releasing caps to MDS mid-write.
  612. *
  613. * If we are synchronous, and write with an old snap context, the OSD
  614. * may return EOLDSNAPC. In that case, retry the write.. _after_
  615. * dropping our cap refs and allowing the pending snap to logically
  616. * complete _before_ this write occurs.
  617. *
  618. * If we are near ENOSPC, write synchronously.
  619. */
  620. static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
  621. unsigned long nr_segs, loff_t pos)
  622. {
  623. struct file *file = iocb->ki_filp;
  624. struct ceph_file_info *fi = file->private_data;
  625. struct inode *inode = file_inode(file);
  626. struct ceph_inode_info *ci = ceph_inode(inode);
  627. struct ceph_osd_client *osdc =
  628. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  629. ssize_t count, written = 0;
  630. int err, want, got;
  631. if (ceph_snap(inode) != CEPH_NOSNAP)
  632. return -EROFS;
  633. mutex_lock(&inode->i_mutex);
  634. err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
  635. if (err)
  636. goto out;
  637. /* We can write back this queue in page reclaim */
  638. current->backing_dev_info = file->f_mapping->backing_dev_info;
  639. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  640. if (err)
  641. goto out;
  642. if (count == 0)
  643. goto out;
  644. err = file_remove_suid(file);
  645. if (err)
  646. goto out;
  647. err = file_update_time(file);
  648. if (err)
  649. goto out;
  650. retry_snap:
  651. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) {
  652. err = -ENOSPC;
  653. goto out;
  654. }
  655. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  656. inode, ceph_vinop(inode), pos, count, inode->i_size);
  657. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  658. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  659. else
  660. want = CEPH_CAP_FILE_BUFFER;
  661. got = 0;
  662. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, pos + count);
  663. if (err < 0)
  664. goto out;
  665. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  666. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  667. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  668. (iocb->ki_filp->f_flags & O_DIRECT) ||
  669. (fi->flags & CEPH_F_SYNC)) {
  670. mutex_unlock(&inode->i_mutex);
  671. written = ceph_sync_write(file, iov->iov_base, count,
  672. pos, &iocb->ki_pos);
  673. if (written == -EOLDSNAPC) {
  674. dout("aio_write %p %llx.%llx %llu~%u"
  675. "got EOLDSNAPC, retrying\n",
  676. inode, ceph_vinop(inode),
  677. pos, (unsigned)iov->iov_len);
  678. mutex_lock(&inode->i_mutex);
  679. goto retry_snap;
  680. }
  681. } else {
  682. /*
  683. * No need to acquire the i_truncate_mutex. Because
  684. * the MDS revokes Fwb caps before sending truncate
  685. * message to us. We can't get Fwb cap while there
  686. * are pending vmtruncate. So write and vmtruncate
  687. * can not run at the same time
  688. */
  689. written = generic_file_buffered_write(iocb, iov, nr_segs,
  690. pos, &iocb->ki_pos,
  691. count, 0);
  692. mutex_unlock(&inode->i_mutex);
  693. }
  694. if (written >= 0) {
  695. int dirty;
  696. spin_lock(&ci->i_ceph_lock);
  697. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  698. spin_unlock(&ci->i_ceph_lock);
  699. if (dirty)
  700. __mark_inode_dirty(inode, dirty);
  701. }
  702. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  703. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  704. ceph_cap_string(got));
  705. ceph_put_cap_refs(ci, got);
  706. if (written >= 0 &&
  707. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) ||
  708. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
  709. err = vfs_fsync_range(file, pos, pos + written - 1, 1);
  710. if (err < 0)
  711. written = err;
  712. }
  713. goto out_unlocked;
  714. out:
  715. mutex_unlock(&inode->i_mutex);
  716. out_unlocked:
  717. current->backing_dev_info = NULL;
  718. return written ? written : err;
  719. }
  720. /*
  721. * llseek. be sure to verify file size on SEEK_END.
  722. */
  723. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  724. {
  725. struct inode *inode = file->f_mapping->host;
  726. int ret;
  727. mutex_lock(&inode->i_mutex);
  728. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  729. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  730. if (ret < 0) {
  731. offset = ret;
  732. goto out;
  733. }
  734. }
  735. switch (whence) {
  736. case SEEK_END:
  737. offset += inode->i_size;
  738. break;
  739. case SEEK_CUR:
  740. /*
  741. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  742. * position-querying operation. Avoid rewriting the "same"
  743. * f_pos value back to the file because a concurrent read(),
  744. * write() or lseek() might have altered it
  745. */
  746. if (offset == 0) {
  747. offset = file->f_pos;
  748. goto out;
  749. }
  750. offset += file->f_pos;
  751. break;
  752. case SEEK_DATA:
  753. if (offset >= inode->i_size) {
  754. ret = -ENXIO;
  755. goto out;
  756. }
  757. break;
  758. case SEEK_HOLE:
  759. if (offset >= inode->i_size) {
  760. ret = -ENXIO;
  761. goto out;
  762. }
  763. offset = inode->i_size;
  764. break;
  765. }
  766. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  767. out:
  768. mutex_unlock(&inode->i_mutex);
  769. return offset;
  770. }
  771. static inline void ceph_zero_partial_page(
  772. struct inode *inode, loff_t offset, unsigned size)
  773. {
  774. struct page *page;
  775. pgoff_t index = offset >> PAGE_CACHE_SHIFT;
  776. page = find_lock_page(inode->i_mapping, index);
  777. if (page) {
  778. wait_on_page_writeback(page);
  779. zero_user(page, offset & (PAGE_CACHE_SIZE - 1), size);
  780. unlock_page(page);
  781. page_cache_release(page);
  782. }
  783. }
  784. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  785. loff_t length)
  786. {
  787. loff_t nearly = round_up(offset, PAGE_CACHE_SIZE);
  788. if (offset < nearly) {
  789. loff_t size = nearly - offset;
  790. if (length < size)
  791. size = length;
  792. ceph_zero_partial_page(inode, offset, size);
  793. offset += size;
  794. length -= size;
  795. }
  796. if (length >= PAGE_CACHE_SIZE) {
  797. loff_t size = round_down(length, PAGE_CACHE_SIZE);
  798. truncate_pagecache_range(inode, offset, offset + size - 1);
  799. offset += size;
  800. length -= size;
  801. }
  802. if (length)
  803. ceph_zero_partial_page(inode, offset, length);
  804. }
  805. static int ceph_zero_partial_object(struct inode *inode,
  806. loff_t offset, loff_t *length)
  807. {
  808. struct ceph_inode_info *ci = ceph_inode(inode);
  809. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  810. struct ceph_osd_request *req;
  811. int ret = 0;
  812. loff_t zero = 0;
  813. int op;
  814. if (!length) {
  815. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  816. length = &zero;
  817. } else {
  818. op = CEPH_OSD_OP_ZERO;
  819. }
  820. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  821. ceph_vino(inode),
  822. offset, length,
  823. 1, op,
  824. CEPH_OSD_FLAG_WRITE |
  825. CEPH_OSD_FLAG_ONDISK,
  826. NULL, 0, 0, false);
  827. if (IS_ERR(req)) {
  828. ret = PTR_ERR(req);
  829. goto out;
  830. }
  831. ceph_osdc_build_request(req, offset, NULL, ceph_vino(inode).snap,
  832. &inode->i_mtime);
  833. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  834. if (!ret) {
  835. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  836. if (ret == -ENOENT)
  837. ret = 0;
  838. }
  839. ceph_osdc_put_request(req);
  840. out:
  841. return ret;
  842. }
  843. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  844. {
  845. int ret = 0;
  846. struct ceph_inode_info *ci = ceph_inode(inode);
  847. s32 stripe_unit = ceph_file_layout_su(ci->i_layout);
  848. s32 stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
  849. s32 object_size = ceph_file_layout_object_size(ci->i_layout);
  850. u64 object_set_size = object_size * stripe_count;
  851. u64 nearly, t;
  852. /* round offset up to next period boundary */
  853. nearly = offset + object_set_size - 1;
  854. t = nearly;
  855. nearly -= do_div(t, object_set_size);
  856. while (length && offset < nearly) {
  857. loff_t size = length;
  858. ret = ceph_zero_partial_object(inode, offset, &size);
  859. if (ret < 0)
  860. return ret;
  861. offset += size;
  862. length -= size;
  863. }
  864. while (length >= object_set_size) {
  865. int i;
  866. loff_t pos = offset;
  867. for (i = 0; i < stripe_count; ++i) {
  868. ret = ceph_zero_partial_object(inode, pos, NULL);
  869. if (ret < 0)
  870. return ret;
  871. pos += stripe_unit;
  872. }
  873. offset += object_set_size;
  874. length -= object_set_size;
  875. }
  876. while (length) {
  877. loff_t size = length;
  878. ret = ceph_zero_partial_object(inode, offset, &size);
  879. if (ret < 0)
  880. return ret;
  881. offset += size;
  882. length -= size;
  883. }
  884. return ret;
  885. }
  886. static long ceph_fallocate(struct file *file, int mode,
  887. loff_t offset, loff_t length)
  888. {
  889. struct ceph_file_info *fi = file->private_data;
  890. struct inode *inode = file->f_dentry->d_inode;
  891. struct ceph_inode_info *ci = ceph_inode(inode);
  892. struct ceph_osd_client *osdc =
  893. &ceph_inode_to_client(inode)->client->osdc;
  894. int want, got = 0;
  895. int dirty;
  896. int ret = 0;
  897. loff_t endoff = 0;
  898. loff_t size;
  899. if (!S_ISREG(inode->i_mode))
  900. return -EOPNOTSUPP;
  901. if (IS_SWAPFILE(inode))
  902. return -ETXTBSY;
  903. mutex_lock(&inode->i_mutex);
  904. if (ceph_snap(inode) != CEPH_NOSNAP) {
  905. ret = -EROFS;
  906. goto unlock;
  907. }
  908. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) &&
  909. !(mode & FALLOC_FL_PUNCH_HOLE)) {
  910. ret = -ENOSPC;
  911. goto unlock;
  912. }
  913. size = i_size_read(inode);
  914. if (!(mode & FALLOC_FL_KEEP_SIZE))
  915. endoff = offset + length;
  916. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  917. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  918. else
  919. want = CEPH_CAP_FILE_BUFFER;
  920. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, endoff);
  921. if (ret < 0)
  922. goto unlock;
  923. if (mode & FALLOC_FL_PUNCH_HOLE) {
  924. if (offset < size)
  925. ceph_zero_pagecache_range(inode, offset, length);
  926. ret = ceph_zero_objects(inode, offset, length);
  927. } else if (endoff > size) {
  928. truncate_pagecache_range(inode, size, -1);
  929. if (ceph_inode_set_size(inode, endoff))
  930. ceph_check_caps(ceph_inode(inode),
  931. CHECK_CAPS_AUTHONLY, NULL);
  932. }
  933. if (!ret) {
  934. spin_lock(&ci->i_ceph_lock);
  935. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  936. spin_unlock(&ci->i_ceph_lock);
  937. if (dirty)
  938. __mark_inode_dirty(inode, dirty);
  939. }
  940. ceph_put_cap_refs(ci, got);
  941. unlock:
  942. mutex_unlock(&inode->i_mutex);
  943. return ret;
  944. }
  945. const struct file_operations ceph_file_fops = {
  946. .open = ceph_open,
  947. .release = ceph_release,
  948. .llseek = ceph_llseek,
  949. .read = do_sync_read,
  950. .write = do_sync_write,
  951. .aio_read = ceph_aio_read,
  952. .aio_write = ceph_aio_write,
  953. .mmap = ceph_mmap,
  954. .fsync = ceph_fsync,
  955. .lock = ceph_lock,
  956. .flock = ceph_flock,
  957. .splice_read = generic_file_splice_read,
  958. .splice_write = generic_file_splice_write,
  959. .unlocked_ioctl = ceph_ioctl,
  960. .compat_ioctl = ceph_ioctl,
  961. .fallocate = ceph_fallocate,
  962. };