file.c 35 KB

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