file.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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. HAVE_RETRIED = 1,
  357. CHECK_EOF = 2,
  358. READ_INLINE = 3,
  359. };
  360. /*
  361. * Read a range of bytes striped over one or more objects. Iterate over
  362. * objects we stripe over. (That's not atomic, but good enough for now.)
  363. *
  364. * If we get a short result from the OSD, check against i_size; we need to
  365. * only return a short read to the caller if we hit EOF.
  366. */
  367. static int striped_read(struct inode *inode,
  368. u64 off, u64 len,
  369. struct page **pages, int num_pages,
  370. int *checkeof)
  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. loff_t i_size;
  376. int page_align, pages_left;
  377. int read, ret;
  378. struct page **page_pos;
  379. bool hit_stripe, was_short;
  380. /*
  381. * we may need to do multiple reads. not atomic, unfortunately.
  382. */
  383. pos = off;
  384. left = len;
  385. page_pos = pages;
  386. pages_left = num_pages;
  387. read = 0;
  388. more:
  389. page_align = pos & ~PAGE_MASK;
  390. this_len = left;
  391. ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
  392. &ci->i_layout, pos, &this_len,
  393. ci->i_truncate_seq,
  394. ci->i_truncate_size,
  395. page_pos, pages_left, page_align);
  396. if (ret == -ENOENT)
  397. ret = 0;
  398. hit_stripe = this_len < left;
  399. was_short = ret >= 0 && ret < this_len;
  400. dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, left, read,
  401. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  402. i_size = i_size_read(inode);
  403. if (ret >= 0) {
  404. int didpages;
  405. if (was_short && (pos + ret < i_size)) {
  406. int zlen = min(this_len - ret, i_size - pos - ret);
  407. int zoff = (off & ~PAGE_MASK) + read + ret;
  408. dout(" zero gap %llu to %llu\n",
  409. pos + ret, pos + ret + zlen);
  410. ceph_zero_page_vector_range(zoff, zlen, pages);
  411. ret += zlen;
  412. }
  413. didpages = (page_align + ret) >> PAGE_CACHE_SHIFT;
  414. pos += ret;
  415. read = pos - off;
  416. left -= ret;
  417. page_pos += didpages;
  418. pages_left -= didpages;
  419. /* hit stripe and need continue*/
  420. if (left && hit_stripe && pos < i_size)
  421. goto more;
  422. }
  423. if (read > 0) {
  424. ret = read;
  425. /* did we bounce off eof? */
  426. if (pos + left > i_size)
  427. *checkeof = CHECK_EOF;
  428. }
  429. dout("striped_read returns %d\n", ret);
  430. return ret;
  431. }
  432. /*
  433. * Completely synchronous read and write methods. Direct from __user
  434. * buffer to osd, or directly to user pages (if O_DIRECT).
  435. *
  436. * If the read spans object boundary, just do multiple reads.
  437. */
  438. static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *i,
  439. int *checkeof)
  440. {
  441. struct file *file = iocb->ki_filp;
  442. struct inode *inode = file_inode(file);
  443. struct page **pages;
  444. u64 off = iocb->ki_pos;
  445. int num_pages, ret;
  446. size_t len = iov_iter_count(i);
  447. dout("sync_read on file %p %llu~%u %s\n", file, off,
  448. (unsigned)len,
  449. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  450. if (!len)
  451. return 0;
  452. /*
  453. * flush any page cache pages in this range. this
  454. * will make concurrent normal and sync io slow,
  455. * but it will at least behave sensibly when they are
  456. * in sequence.
  457. */
  458. ret = filemap_write_and_wait_range(inode->i_mapping, off,
  459. off + len);
  460. if (ret < 0)
  461. return ret;
  462. num_pages = calc_pages_for(off, len);
  463. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  464. if (IS_ERR(pages))
  465. return PTR_ERR(pages);
  466. ret = striped_read(inode, off, len, pages,
  467. num_pages, checkeof);
  468. if (ret > 0) {
  469. int l, k = 0;
  470. size_t left = ret;
  471. while (left) {
  472. size_t page_off = off & ~PAGE_MASK;
  473. size_t copy = min_t(size_t, left,
  474. PAGE_SIZE - page_off);
  475. l = copy_page_to_iter(pages[k++], page_off, copy, i);
  476. off += l;
  477. left -= l;
  478. if (l < copy)
  479. break;
  480. }
  481. }
  482. ceph_release_page_vector(pages, num_pages);
  483. if (off > iocb->ki_pos) {
  484. ret = off - iocb->ki_pos;
  485. iocb->ki_pos = off;
  486. }
  487. dout("sync_read result %d\n", ret);
  488. return ret;
  489. }
  490. struct ceph_aio_request {
  491. struct kiocb *iocb;
  492. size_t total_len;
  493. int write;
  494. int error;
  495. struct list_head osd_reqs;
  496. unsigned num_reqs;
  497. atomic_t pending_reqs;
  498. struct timespec mtime;
  499. struct ceph_cap_flush *prealloc_cf;
  500. };
  501. struct ceph_aio_work {
  502. struct work_struct work;
  503. struct ceph_osd_request *req;
  504. };
  505. static void ceph_aio_retry_work(struct work_struct *work);
  506. static void ceph_aio_complete(struct inode *inode,
  507. struct ceph_aio_request *aio_req)
  508. {
  509. struct ceph_inode_info *ci = ceph_inode(inode);
  510. int ret;
  511. if (!atomic_dec_and_test(&aio_req->pending_reqs))
  512. return;
  513. ret = aio_req->error;
  514. if (!ret)
  515. ret = aio_req->total_len;
  516. dout("ceph_aio_complete %p rc %d\n", inode, ret);
  517. if (ret >= 0 && aio_req->write) {
  518. int dirty;
  519. loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
  520. if (endoff > i_size_read(inode)) {
  521. if (ceph_inode_set_size(inode, endoff))
  522. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  523. }
  524. spin_lock(&ci->i_ceph_lock);
  525. ci->i_inline_version = CEPH_INLINE_NONE;
  526. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  527. &aio_req->prealloc_cf);
  528. spin_unlock(&ci->i_ceph_lock);
  529. if (dirty)
  530. __mark_inode_dirty(inode, dirty);
  531. }
  532. ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
  533. CEPH_CAP_FILE_RD));
  534. aio_req->iocb->ki_complete(aio_req->iocb, ret, 0);
  535. ceph_free_cap_flush(aio_req->prealloc_cf);
  536. kfree(aio_req);
  537. }
  538. static void ceph_aio_complete_req(struct ceph_osd_request *req,
  539. struct ceph_msg *msg)
  540. {
  541. int rc = req->r_result;
  542. struct inode *inode = req->r_inode;
  543. struct ceph_aio_request *aio_req = req->r_priv;
  544. struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
  545. int num_pages = calc_pages_for((u64)osd_data->alignment,
  546. osd_data->length);
  547. dout("ceph_aio_complete_req %p rc %d bytes %llu\n",
  548. inode, rc, osd_data->length);
  549. if (rc == -EOLDSNAPC) {
  550. struct ceph_aio_work *aio_work;
  551. BUG_ON(!aio_req->write);
  552. aio_work = kmalloc(sizeof(*aio_work), GFP_NOFS);
  553. if (aio_work) {
  554. INIT_WORK(&aio_work->work, ceph_aio_retry_work);
  555. aio_work->req = req;
  556. queue_work(ceph_inode_to_client(inode)->wb_wq,
  557. &aio_work->work);
  558. return;
  559. }
  560. rc = -ENOMEM;
  561. } else if (!aio_req->write) {
  562. if (rc == -ENOENT)
  563. rc = 0;
  564. if (rc >= 0 && osd_data->length > rc) {
  565. int zoff = osd_data->alignment + rc;
  566. int zlen = osd_data->length - rc;
  567. /*
  568. * If read is satisfied by single OSD request,
  569. * it can pass EOF. Otherwise read is within
  570. * i_size.
  571. */
  572. if (aio_req->num_reqs == 1) {
  573. loff_t i_size = i_size_read(inode);
  574. loff_t endoff = aio_req->iocb->ki_pos + rc;
  575. if (endoff < i_size)
  576. zlen = min_t(size_t, zlen,
  577. i_size - endoff);
  578. aio_req->total_len = rc + zlen;
  579. }
  580. if (zlen > 0)
  581. ceph_zero_page_vector_range(zoff, zlen,
  582. osd_data->pages);
  583. }
  584. }
  585. ceph_put_page_vector(osd_data->pages, num_pages, false);
  586. ceph_osdc_put_request(req);
  587. if (rc < 0)
  588. cmpxchg(&aio_req->error, 0, rc);
  589. ceph_aio_complete(inode, aio_req);
  590. return;
  591. }
  592. static void ceph_aio_retry_work(struct work_struct *work)
  593. {
  594. struct ceph_aio_work *aio_work =
  595. container_of(work, struct ceph_aio_work, work);
  596. struct ceph_osd_request *orig_req = aio_work->req;
  597. struct ceph_aio_request *aio_req = orig_req->r_priv;
  598. struct inode *inode = orig_req->r_inode;
  599. struct ceph_inode_info *ci = ceph_inode(inode);
  600. struct ceph_snap_context *snapc;
  601. struct ceph_osd_request *req;
  602. int ret;
  603. spin_lock(&ci->i_ceph_lock);
  604. if (__ceph_have_pending_cap_snap(ci)) {
  605. struct ceph_cap_snap *capsnap =
  606. list_last_entry(&ci->i_cap_snaps,
  607. struct ceph_cap_snap,
  608. ci_item);
  609. snapc = ceph_get_snap_context(capsnap->context);
  610. } else {
  611. BUG_ON(!ci->i_head_snapc);
  612. snapc = ceph_get_snap_context(ci->i_head_snapc);
  613. }
  614. spin_unlock(&ci->i_ceph_lock);
  615. req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 2,
  616. false, GFP_NOFS);
  617. if (IS_ERR(req)) {
  618. ret = PTR_ERR(req);
  619. req = orig_req;
  620. goto out;
  621. }
  622. req->r_flags = CEPH_OSD_FLAG_ORDERSNAP |
  623. CEPH_OSD_FLAG_ONDISK |
  624. CEPH_OSD_FLAG_WRITE;
  625. req->r_base_oloc = orig_req->r_base_oloc;
  626. req->r_base_oid = orig_req->r_base_oid;
  627. req->r_ops[0] = orig_req->r_ops[0];
  628. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC, 0);
  629. ceph_osdc_build_request(req, req->r_ops[0].extent.offset,
  630. snapc, CEPH_NOSNAP, &aio_req->mtime);
  631. ceph_put_snap_context(snapc);
  632. ceph_osdc_put_request(orig_req);
  633. req->r_callback = ceph_aio_complete_req;
  634. req->r_inode = inode;
  635. req->r_priv = aio_req;
  636. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  637. out:
  638. if (ret < 0) {
  639. BUG_ON(ret == -EOLDSNAPC);
  640. req->r_result = ret;
  641. ceph_aio_complete_req(req, NULL);
  642. }
  643. kfree(aio_work);
  644. }
  645. /*
  646. * Write commit request unsafe callback, called to tell us when a
  647. * request is unsafe (that is, in flight--has been handed to the
  648. * messenger to send to its target osd). It is called again when
  649. * we've received a response message indicating the request is
  650. * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
  651. * is completed early (and unsuccessfully) due to a timeout or
  652. * interrupt.
  653. *
  654. * This is used if we requested both an ACK and ONDISK commit reply
  655. * from the OSD.
  656. */
  657. static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
  658. {
  659. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  660. dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
  661. unsafe ? "un" : "");
  662. if (unsafe) {
  663. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  664. spin_lock(&ci->i_unsafe_lock);
  665. list_add_tail(&req->r_unsafe_item,
  666. &ci->i_unsafe_writes);
  667. spin_unlock(&ci->i_unsafe_lock);
  668. } else {
  669. spin_lock(&ci->i_unsafe_lock);
  670. list_del_init(&req->r_unsafe_item);
  671. spin_unlock(&ci->i_unsafe_lock);
  672. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  673. }
  674. }
  675. static ssize_t
  676. ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
  677. struct ceph_snap_context *snapc,
  678. struct ceph_cap_flush **pcf)
  679. {
  680. struct file *file = iocb->ki_filp;
  681. struct inode *inode = file_inode(file);
  682. struct ceph_inode_info *ci = ceph_inode(inode);
  683. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  684. struct ceph_vino vino;
  685. struct ceph_osd_request *req;
  686. struct page **pages;
  687. struct ceph_aio_request *aio_req = NULL;
  688. int num_pages = 0;
  689. int flags;
  690. int ret;
  691. struct timespec mtime = CURRENT_TIME;
  692. size_t count = iov_iter_count(iter);
  693. loff_t pos = iocb->ki_pos;
  694. bool write = iov_iter_rw(iter) == WRITE;
  695. if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  696. return -EROFS;
  697. dout("sync_direct_read_write (%s) on file %p %lld~%u\n",
  698. (write ? "write" : "read"), file, pos, (unsigned)count);
  699. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  700. if (ret < 0)
  701. return ret;
  702. if (write) {
  703. ret = invalidate_inode_pages2_range(inode->i_mapping,
  704. pos >> PAGE_CACHE_SHIFT,
  705. (pos + count) >> PAGE_CACHE_SHIFT);
  706. if (ret < 0)
  707. dout("invalidate_inode_pages2_range returned %d\n", ret);
  708. flags = CEPH_OSD_FLAG_ORDERSNAP |
  709. CEPH_OSD_FLAG_ONDISK |
  710. CEPH_OSD_FLAG_WRITE;
  711. } else {
  712. flags = CEPH_OSD_FLAG_READ;
  713. }
  714. while (iov_iter_count(iter) > 0) {
  715. u64 size = dio_get_pagev_size(iter);
  716. size_t start = 0;
  717. ssize_t len;
  718. vino = ceph_vino(inode);
  719. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  720. vino, pos, &size, 0,
  721. /*include a 'startsync' command*/
  722. write ? 2 : 1,
  723. write ? CEPH_OSD_OP_WRITE :
  724. CEPH_OSD_OP_READ,
  725. flags, snapc,
  726. ci->i_truncate_seq,
  727. ci->i_truncate_size,
  728. false);
  729. if (IS_ERR(req)) {
  730. ret = PTR_ERR(req);
  731. break;
  732. }
  733. len = size;
  734. pages = dio_get_pages_alloc(iter, len, &start, &num_pages);
  735. if (IS_ERR(pages)) {
  736. ceph_osdc_put_request(req);
  737. ret = PTR_ERR(pages);
  738. break;
  739. }
  740. /*
  741. * To simplify error handling, allow AIO when IO within i_size
  742. * or IO can be satisfied by single OSD request.
  743. */
  744. if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
  745. (len == count || pos + count <= i_size_read(inode))) {
  746. aio_req = kzalloc(sizeof(*aio_req), GFP_KERNEL);
  747. if (aio_req) {
  748. aio_req->iocb = iocb;
  749. aio_req->write = write;
  750. INIT_LIST_HEAD(&aio_req->osd_reqs);
  751. if (write) {
  752. aio_req->mtime = mtime;
  753. swap(aio_req->prealloc_cf, *pcf);
  754. }
  755. }
  756. /* ignore error */
  757. }
  758. if (write) {
  759. /*
  760. * throw out any page cache pages in this range. this
  761. * may block.
  762. */
  763. truncate_inode_pages_range(inode->i_mapping, pos,
  764. (pos+len) | (PAGE_CACHE_SIZE - 1));
  765. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC, 0);
  766. }
  767. osd_req_op_extent_osd_data_pages(req, 0, pages, len, start,
  768. false, false);
  769. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  770. if (aio_req) {
  771. aio_req->total_len += len;
  772. aio_req->num_reqs++;
  773. atomic_inc(&aio_req->pending_reqs);
  774. req->r_callback = ceph_aio_complete_req;
  775. req->r_inode = inode;
  776. req->r_priv = aio_req;
  777. list_add_tail(&req->r_unsafe_item, &aio_req->osd_reqs);
  778. pos += len;
  779. iov_iter_advance(iter, len);
  780. continue;
  781. }
  782. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  783. if (!ret)
  784. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  785. size = i_size_read(inode);
  786. if (!write) {
  787. if (ret == -ENOENT)
  788. ret = 0;
  789. if (ret >= 0 && ret < len && pos + ret < size) {
  790. int zlen = min_t(size_t, len - ret,
  791. size - pos - ret);
  792. ceph_zero_page_vector_range(start + ret, zlen,
  793. pages);
  794. ret += zlen;
  795. }
  796. if (ret >= 0)
  797. len = ret;
  798. }
  799. ceph_put_page_vector(pages, num_pages, false);
  800. ceph_osdc_put_request(req);
  801. if (ret < 0)
  802. break;
  803. pos += len;
  804. iov_iter_advance(iter, len);
  805. if (!write && pos >= size)
  806. break;
  807. if (write && pos > size) {
  808. if (ceph_inode_set_size(inode, pos))
  809. ceph_check_caps(ceph_inode(inode),
  810. CHECK_CAPS_AUTHONLY,
  811. NULL);
  812. }
  813. }
  814. if (aio_req) {
  815. if (aio_req->num_reqs == 0) {
  816. kfree(aio_req);
  817. return ret;
  818. }
  819. ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
  820. CEPH_CAP_FILE_RD);
  821. while (!list_empty(&aio_req->osd_reqs)) {
  822. req = list_first_entry(&aio_req->osd_reqs,
  823. struct ceph_osd_request,
  824. r_unsafe_item);
  825. list_del_init(&req->r_unsafe_item);
  826. if (ret >= 0)
  827. ret = ceph_osdc_start_request(req->r_osdc,
  828. req, false);
  829. if (ret < 0) {
  830. BUG_ON(ret == -EOLDSNAPC);
  831. req->r_result = ret;
  832. ceph_aio_complete_req(req, NULL);
  833. }
  834. }
  835. return -EIOCBQUEUED;
  836. }
  837. if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
  838. ret = pos - iocb->ki_pos;
  839. iocb->ki_pos = pos;
  840. }
  841. return ret;
  842. }
  843. /*
  844. * Synchronous write, straight from __user pointer or user pages.
  845. *
  846. * If write spans object boundary, just do multiple writes. (For a
  847. * correct atomic write, we should e.g. take write locks on all
  848. * objects, rollback on failure, etc.)
  849. */
  850. static ssize_t
  851. ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
  852. struct ceph_snap_context *snapc)
  853. {
  854. struct file *file = iocb->ki_filp;
  855. struct inode *inode = file_inode(file);
  856. struct ceph_inode_info *ci = ceph_inode(inode);
  857. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  858. struct ceph_vino vino;
  859. struct ceph_osd_request *req;
  860. struct page **pages;
  861. u64 len;
  862. int num_pages;
  863. int written = 0;
  864. int flags;
  865. int check_caps = 0;
  866. int ret;
  867. struct timespec mtime = CURRENT_TIME;
  868. size_t count = iov_iter_count(from);
  869. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  870. return -EROFS;
  871. dout("sync_write on file %p %lld~%u\n", file, pos, (unsigned)count);
  872. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  873. if (ret < 0)
  874. return ret;
  875. ret = invalidate_inode_pages2_range(inode->i_mapping,
  876. pos >> PAGE_CACHE_SHIFT,
  877. (pos + count) >> PAGE_CACHE_SHIFT);
  878. if (ret < 0)
  879. dout("invalidate_inode_pages2_range returned %d\n", ret);
  880. flags = CEPH_OSD_FLAG_ORDERSNAP |
  881. CEPH_OSD_FLAG_ONDISK |
  882. CEPH_OSD_FLAG_WRITE |
  883. CEPH_OSD_FLAG_ACK;
  884. while ((len = iov_iter_count(from)) > 0) {
  885. size_t left;
  886. int n;
  887. vino = ceph_vino(inode);
  888. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  889. vino, pos, &len, 0, 1,
  890. CEPH_OSD_OP_WRITE, flags, snapc,
  891. ci->i_truncate_seq,
  892. ci->i_truncate_size,
  893. false);
  894. if (IS_ERR(req)) {
  895. ret = PTR_ERR(req);
  896. break;
  897. }
  898. /*
  899. * write from beginning of first page,
  900. * regardless of io alignment
  901. */
  902. num_pages = (len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  903. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  904. if (IS_ERR(pages)) {
  905. ret = PTR_ERR(pages);
  906. goto out;
  907. }
  908. left = len;
  909. for (n = 0; n < num_pages; n++) {
  910. size_t plen = min_t(size_t, left, PAGE_SIZE);
  911. ret = copy_page_from_iter(pages[n], 0, plen, from);
  912. if (ret != plen) {
  913. ret = -EFAULT;
  914. break;
  915. }
  916. left -= ret;
  917. }
  918. if (ret < 0) {
  919. ceph_release_page_vector(pages, num_pages);
  920. goto out;
  921. }
  922. /* get a second commit callback */
  923. req->r_unsafe_callback = ceph_sync_write_unsafe;
  924. req->r_inode = inode;
  925. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
  926. false, true);
  927. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  928. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  929. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  930. if (!ret)
  931. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  932. out:
  933. ceph_osdc_put_request(req);
  934. if (ret == 0) {
  935. pos += len;
  936. written += len;
  937. if (pos > i_size_read(inode)) {
  938. check_caps = ceph_inode_set_size(inode, pos);
  939. if (check_caps)
  940. ceph_check_caps(ceph_inode(inode),
  941. CHECK_CAPS_AUTHONLY,
  942. NULL);
  943. }
  944. } else
  945. break;
  946. }
  947. if (ret != -EOLDSNAPC && written > 0) {
  948. ret = written;
  949. iocb->ki_pos = pos;
  950. }
  951. return ret;
  952. }
  953. /*
  954. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  955. * Atomically grab references, so that those bits are not released
  956. * back to the MDS mid-read.
  957. *
  958. * Hmm, the sync read case isn't actually async... should it be?
  959. */
  960. static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
  961. {
  962. struct file *filp = iocb->ki_filp;
  963. struct ceph_file_info *fi = filp->private_data;
  964. size_t len = iov_iter_count(to);
  965. struct inode *inode = file_inode(filp);
  966. struct ceph_inode_info *ci = ceph_inode(inode);
  967. struct page *pinned_page = NULL;
  968. ssize_t ret;
  969. int want, got = 0;
  970. int retry_op = 0, read = 0;
  971. again:
  972. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  973. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
  974. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  975. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  976. else
  977. want = CEPH_CAP_FILE_CACHE;
  978. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
  979. if (ret < 0)
  980. return ret;
  981. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  982. (iocb->ki_flags & IOCB_DIRECT) ||
  983. (fi->flags & CEPH_F_SYNC)) {
  984. dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  985. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  986. ceph_cap_string(got));
  987. if (ci->i_inline_version == CEPH_INLINE_NONE) {
  988. if (!retry_op && (iocb->ki_flags & IOCB_DIRECT)) {
  989. ret = ceph_direct_read_write(iocb, to,
  990. NULL, NULL);
  991. if (ret >= 0 && ret < len)
  992. retry_op = CHECK_EOF;
  993. } else {
  994. ret = ceph_sync_read(iocb, to, &retry_op);
  995. }
  996. } else {
  997. retry_op = READ_INLINE;
  998. }
  999. } else {
  1000. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  1001. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1002. ceph_cap_string(got));
  1003. ret = generic_file_read_iter(iocb, to);
  1004. }
  1005. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  1006. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  1007. if (pinned_page) {
  1008. page_cache_release(pinned_page);
  1009. pinned_page = NULL;
  1010. }
  1011. ceph_put_cap_refs(ci, got);
  1012. if (retry_op > HAVE_RETRIED && ret >= 0) {
  1013. int statret;
  1014. struct page *page = NULL;
  1015. loff_t i_size;
  1016. if (retry_op == READ_INLINE) {
  1017. page = __page_cache_alloc(GFP_KERNEL);
  1018. if (!page)
  1019. return -ENOMEM;
  1020. }
  1021. statret = __ceph_do_getattr(inode, page,
  1022. CEPH_STAT_CAP_INLINE_DATA, !!page);
  1023. if (statret < 0) {
  1024. __free_page(page);
  1025. if (statret == -ENODATA) {
  1026. BUG_ON(retry_op != READ_INLINE);
  1027. goto again;
  1028. }
  1029. return statret;
  1030. }
  1031. i_size = i_size_read(inode);
  1032. if (retry_op == READ_INLINE) {
  1033. BUG_ON(ret > 0 || read > 0);
  1034. if (iocb->ki_pos < i_size &&
  1035. iocb->ki_pos < PAGE_CACHE_SIZE) {
  1036. loff_t end = min_t(loff_t, i_size,
  1037. iocb->ki_pos + len);
  1038. end = min_t(loff_t, end, PAGE_CACHE_SIZE);
  1039. if (statret < end)
  1040. zero_user_segment(page, statret, end);
  1041. ret = copy_page_to_iter(page,
  1042. iocb->ki_pos & ~PAGE_MASK,
  1043. end - iocb->ki_pos, to);
  1044. iocb->ki_pos += ret;
  1045. read += ret;
  1046. }
  1047. if (iocb->ki_pos < i_size && read < len) {
  1048. size_t zlen = min_t(size_t, len - read,
  1049. i_size - iocb->ki_pos);
  1050. ret = iov_iter_zero(zlen, to);
  1051. iocb->ki_pos += ret;
  1052. read += ret;
  1053. }
  1054. __free_pages(page, 0);
  1055. return read;
  1056. }
  1057. /* hit EOF or hole? */
  1058. if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
  1059. ret < len) {
  1060. dout("sync_read hit hole, ppos %lld < size %lld"
  1061. ", reading more\n", iocb->ki_pos, i_size);
  1062. read += ret;
  1063. len -= ret;
  1064. retry_op = HAVE_RETRIED;
  1065. goto again;
  1066. }
  1067. }
  1068. if (ret >= 0)
  1069. ret += read;
  1070. return ret;
  1071. }
  1072. /*
  1073. * Take cap references to avoid releasing caps to MDS mid-write.
  1074. *
  1075. * If we are synchronous, and write with an old snap context, the OSD
  1076. * may return EOLDSNAPC. In that case, retry the write.. _after_
  1077. * dropping our cap refs and allowing the pending snap to logically
  1078. * complete _before_ this write occurs.
  1079. *
  1080. * If we are near ENOSPC, write synchronously.
  1081. */
  1082. static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1083. {
  1084. struct file *file = iocb->ki_filp;
  1085. struct ceph_file_info *fi = file->private_data;
  1086. struct inode *inode = file_inode(file);
  1087. struct ceph_inode_info *ci = ceph_inode(inode);
  1088. struct ceph_osd_client *osdc =
  1089. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  1090. struct ceph_cap_flush *prealloc_cf;
  1091. ssize_t count, written = 0;
  1092. int err, want, got;
  1093. loff_t pos;
  1094. if (ceph_snap(inode) != CEPH_NOSNAP)
  1095. return -EROFS;
  1096. prealloc_cf = ceph_alloc_cap_flush();
  1097. if (!prealloc_cf)
  1098. return -ENOMEM;
  1099. inode_lock(inode);
  1100. /* We can write back this queue in page reclaim */
  1101. current->backing_dev_info = inode_to_bdi(inode);
  1102. if (iocb->ki_flags & IOCB_APPEND) {
  1103. err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1104. if (err < 0)
  1105. goto out;
  1106. }
  1107. err = generic_write_checks(iocb, from);
  1108. if (err <= 0)
  1109. goto out;
  1110. pos = iocb->ki_pos;
  1111. count = iov_iter_count(from);
  1112. err = file_remove_privs(file);
  1113. if (err)
  1114. goto out;
  1115. err = file_update_time(file);
  1116. if (err)
  1117. goto out;
  1118. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1119. err = ceph_uninline_data(file, NULL);
  1120. if (err < 0)
  1121. goto out;
  1122. }
  1123. retry_snap:
  1124. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) {
  1125. err = -ENOSPC;
  1126. goto out;
  1127. }
  1128. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  1129. inode, ceph_vinop(inode), pos, count, i_size_read(inode));
  1130. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1131. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1132. else
  1133. want = CEPH_CAP_FILE_BUFFER;
  1134. got = 0;
  1135. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, pos + count,
  1136. &got, NULL);
  1137. if (err < 0)
  1138. goto out;
  1139. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  1140. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  1141. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  1142. (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC)) {
  1143. struct ceph_snap_context *snapc;
  1144. struct iov_iter data;
  1145. inode_unlock(inode);
  1146. spin_lock(&ci->i_ceph_lock);
  1147. if (__ceph_have_pending_cap_snap(ci)) {
  1148. struct ceph_cap_snap *capsnap =
  1149. list_last_entry(&ci->i_cap_snaps,
  1150. struct ceph_cap_snap,
  1151. ci_item);
  1152. snapc = ceph_get_snap_context(capsnap->context);
  1153. } else {
  1154. BUG_ON(!ci->i_head_snapc);
  1155. snapc = ceph_get_snap_context(ci->i_head_snapc);
  1156. }
  1157. spin_unlock(&ci->i_ceph_lock);
  1158. /* we might need to revert back to that point */
  1159. data = *from;
  1160. if (iocb->ki_flags & IOCB_DIRECT)
  1161. written = ceph_direct_read_write(iocb, &data, snapc,
  1162. &prealloc_cf);
  1163. else
  1164. written = ceph_sync_write(iocb, &data, pos, snapc);
  1165. if (written == -EOLDSNAPC) {
  1166. dout("aio_write %p %llx.%llx %llu~%u"
  1167. "got EOLDSNAPC, retrying\n",
  1168. inode, ceph_vinop(inode),
  1169. pos, (unsigned)count);
  1170. inode_lock(inode);
  1171. goto retry_snap;
  1172. }
  1173. if (written > 0)
  1174. iov_iter_advance(from, written);
  1175. ceph_put_snap_context(snapc);
  1176. } else {
  1177. loff_t old_size = i_size_read(inode);
  1178. /*
  1179. * No need to acquire the i_truncate_mutex. Because
  1180. * the MDS revokes Fwb caps before sending truncate
  1181. * message to us. We can't get Fwb cap while there
  1182. * are pending vmtruncate. So write and vmtruncate
  1183. * can not run at the same time
  1184. */
  1185. written = generic_perform_write(file, from, pos);
  1186. if (likely(written >= 0))
  1187. iocb->ki_pos = pos + written;
  1188. if (i_size_read(inode) > old_size)
  1189. ceph_fscache_update_objectsize(inode);
  1190. inode_unlock(inode);
  1191. }
  1192. if (written >= 0) {
  1193. int dirty;
  1194. spin_lock(&ci->i_ceph_lock);
  1195. ci->i_inline_version = CEPH_INLINE_NONE;
  1196. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1197. &prealloc_cf);
  1198. spin_unlock(&ci->i_ceph_lock);
  1199. if (dirty)
  1200. __mark_inode_dirty(inode, dirty);
  1201. }
  1202. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  1203. inode, ceph_vinop(inode), pos, (unsigned)count,
  1204. ceph_cap_string(got));
  1205. ceph_put_cap_refs(ci, got);
  1206. if (written >= 0 &&
  1207. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) ||
  1208. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
  1209. err = vfs_fsync_range(file, pos, pos + written - 1, 1);
  1210. if (err < 0)
  1211. written = err;
  1212. }
  1213. goto out_unlocked;
  1214. out:
  1215. inode_unlock(inode);
  1216. out_unlocked:
  1217. ceph_free_cap_flush(prealloc_cf);
  1218. current->backing_dev_info = NULL;
  1219. return written ? written : err;
  1220. }
  1221. /*
  1222. * llseek. be sure to verify file size on SEEK_END.
  1223. */
  1224. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  1225. {
  1226. struct inode *inode = file->f_mapping->host;
  1227. loff_t i_size;
  1228. int ret;
  1229. inode_lock(inode);
  1230. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  1231. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1232. if (ret < 0) {
  1233. offset = ret;
  1234. goto out;
  1235. }
  1236. }
  1237. i_size = i_size_read(inode);
  1238. switch (whence) {
  1239. case SEEK_END:
  1240. offset += i_size;
  1241. break;
  1242. case SEEK_CUR:
  1243. /*
  1244. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  1245. * position-querying operation. Avoid rewriting the "same"
  1246. * f_pos value back to the file because a concurrent read(),
  1247. * write() or lseek() might have altered it
  1248. */
  1249. if (offset == 0) {
  1250. offset = file->f_pos;
  1251. goto out;
  1252. }
  1253. offset += file->f_pos;
  1254. break;
  1255. case SEEK_DATA:
  1256. if (offset >= i_size) {
  1257. ret = -ENXIO;
  1258. goto out;
  1259. }
  1260. break;
  1261. case SEEK_HOLE:
  1262. if (offset >= i_size) {
  1263. ret = -ENXIO;
  1264. goto out;
  1265. }
  1266. offset = i_size;
  1267. break;
  1268. }
  1269. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  1270. out:
  1271. inode_unlock(inode);
  1272. return offset;
  1273. }
  1274. static inline void ceph_zero_partial_page(
  1275. struct inode *inode, loff_t offset, unsigned size)
  1276. {
  1277. struct page *page;
  1278. pgoff_t index = offset >> PAGE_CACHE_SHIFT;
  1279. page = find_lock_page(inode->i_mapping, index);
  1280. if (page) {
  1281. wait_on_page_writeback(page);
  1282. zero_user(page, offset & (PAGE_CACHE_SIZE - 1), size);
  1283. unlock_page(page);
  1284. page_cache_release(page);
  1285. }
  1286. }
  1287. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  1288. loff_t length)
  1289. {
  1290. loff_t nearly = round_up(offset, PAGE_CACHE_SIZE);
  1291. if (offset < nearly) {
  1292. loff_t size = nearly - offset;
  1293. if (length < size)
  1294. size = length;
  1295. ceph_zero_partial_page(inode, offset, size);
  1296. offset += size;
  1297. length -= size;
  1298. }
  1299. if (length >= PAGE_CACHE_SIZE) {
  1300. loff_t size = round_down(length, PAGE_CACHE_SIZE);
  1301. truncate_pagecache_range(inode, offset, offset + size - 1);
  1302. offset += size;
  1303. length -= size;
  1304. }
  1305. if (length)
  1306. ceph_zero_partial_page(inode, offset, length);
  1307. }
  1308. static int ceph_zero_partial_object(struct inode *inode,
  1309. loff_t offset, loff_t *length)
  1310. {
  1311. struct ceph_inode_info *ci = ceph_inode(inode);
  1312. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1313. struct ceph_osd_request *req;
  1314. int ret = 0;
  1315. loff_t zero = 0;
  1316. int op;
  1317. if (!length) {
  1318. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  1319. length = &zero;
  1320. } else {
  1321. op = CEPH_OSD_OP_ZERO;
  1322. }
  1323. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1324. ceph_vino(inode),
  1325. offset, length,
  1326. 0, 1, op,
  1327. CEPH_OSD_FLAG_WRITE |
  1328. CEPH_OSD_FLAG_ONDISK,
  1329. NULL, 0, 0, false);
  1330. if (IS_ERR(req)) {
  1331. ret = PTR_ERR(req);
  1332. goto out;
  1333. }
  1334. ceph_osdc_build_request(req, offset, NULL, ceph_vino(inode).snap,
  1335. &inode->i_mtime);
  1336. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1337. if (!ret) {
  1338. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1339. if (ret == -ENOENT)
  1340. ret = 0;
  1341. }
  1342. ceph_osdc_put_request(req);
  1343. out:
  1344. return ret;
  1345. }
  1346. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  1347. {
  1348. int ret = 0;
  1349. struct ceph_inode_info *ci = ceph_inode(inode);
  1350. s32 stripe_unit = ceph_file_layout_su(ci->i_layout);
  1351. s32 stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
  1352. s32 object_size = ceph_file_layout_object_size(ci->i_layout);
  1353. u64 object_set_size = object_size * stripe_count;
  1354. u64 nearly, t;
  1355. /* round offset up to next period boundary */
  1356. nearly = offset + object_set_size - 1;
  1357. t = nearly;
  1358. nearly -= do_div(t, object_set_size);
  1359. while (length && offset < nearly) {
  1360. loff_t size = length;
  1361. ret = ceph_zero_partial_object(inode, offset, &size);
  1362. if (ret < 0)
  1363. return ret;
  1364. offset += size;
  1365. length -= size;
  1366. }
  1367. while (length >= object_set_size) {
  1368. int i;
  1369. loff_t pos = offset;
  1370. for (i = 0; i < stripe_count; ++i) {
  1371. ret = ceph_zero_partial_object(inode, pos, NULL);
  1372. if (ret < 0)
  1373. return ret;
  1374. pos += stripe_unit;
  1375. }
  1376. offset += object_set_size;
  1377. length -= object_set_size;
  1378. }
  1379. while (length) {
  1380. loff_t size = length;
  1381. ret = ceph_zero_partial_object(inode, offset, &size);
  1382. if (ret < 0)
  1383. return ret;
  1384. offset += size;
  1385. length -= size;
  1386. }
  1387. return ret;
  1388. }
  1389. static long ceph_fallocate(struct file *file, int mode,
  1390. loff_t offset, loff_t length)
  1391. {
  1392. struct ceph_file_info *fi = file->private_data;
  1393. struct inode *inode = file_inode(file);
  1394. struct ceph_inode_info *ci = ceph_inode(inode);
  1395. struct ceph_osd_client *osdc =
  1396. &ceph_inode_to_client(inode)->client->osdc;
  1397. struct ceph_cap_flush *prealloc_cf;
  1398. int want, got = 0;
  1399. int dirty;
  1400. int ret = 0;
  1401. loff_t endoff = 0;
  1402. loff_t size;
  1403. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1404. return -EOPNOTSUPP;
  1405. if (!S_ISREG(inode->i_mode))
  1406. return -EOPNOTSUPP;
  1407. prealloc_cf = ceph_alloc_cap_flush();
  1408. if (!prealloc_cf)
  1409. return -ENOMEM;
  1410. inode_lock(inode);
  1411. if (ceph_snap(inode) != CEPH_NOSNAP) {
  1412. ret = -EROFS;
  1413. goto unlock;
  1414. }
  1415. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) &&
  1416. !(mode & FALLOC_FL_PUNCH_HOLE)) {
  1417. ret = -ENOSPC;
  1418. goto unlock;
  1419. }
  1420. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1421. ret = ceph_uninline_data(file, NULL);
  1422. if (ret < 0)
  1423. goto unlock;
  1424. }
  1425. size = i_size_read(inode);
  1426. if (!(mode & FALLOC_FL_KEEP_SIZE))
  1427. endoff = offset + length;
  1428. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1429. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1430. else
  1431. want = CEPH_CAP_FILE_BUFFER;
  1432. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, endoff, &got, NULL);
  1433. if (ret < 0)
  1434. goto unlock;
  1435. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1436. if (offset < size)
  1437. ceph_zero_pagecache_range(inode, offset, length);
  1438. ret = ceph_zero_objects(inode, offset, length);
  1439. } else if (endoff > size) {
  1440. truncate_pagecache_range(inode, size, -1);
  1441. if (ceph_inode_set_size(inode, endoff))
  1442. ceph_check_caps(ceph_inode(inode),
  1443. CHECK_CAPS_AUTHONLY, NULL);
  1444. }
  1445. if (!ret) {
  1446. spin_lock(&ci->i_ceph_lock);
  1447. ci->i_inline_version = CEPH_INLINE_NONE;
  1448. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1449. &prealloc_cf);
  1450. spin_unlock(&ci->i_ceph_lock);
  1451. if (dirty)
  1452. __mark_inode_dirty(inode, dirty);
  1453. }
  1454. ceph_put_cap_refs(ci, got);
  1455. unlock:
  1456. inode_unlock(inode);
  1457. ceph_free_cap_flush(prealloc_cf);
  1458. return ret;
  1459. }
  1460. const struct file_operations ceph_file_fops = {
  1461. .open = ceph_open,
  1462. .release = ceph_release,
  1463. .llseek = ceph_llseek,
  1464. .read_iter = ceph_read_iter,
  1465. .write_iter = ceph_write_iter,
  1466. .mmap = ceph_mmap,
  1467. .fsync = ceph_fsync,
  1468. .lock = ceph_lock,
  1469. .flock = ceph_flock,
  1470. .splice_read = generic_file_splice_read,
  1471. .splice_write = iter_file_splice_write,
  1472. .unlocked_ioctl = ceph_ioctl,
  1473. .compat_ioctl = ceph_ioctl,
  1474. .fallocate = ceph_fallocate,
  1475. };