file.c 37 KB

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