file.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  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. switch (inode->i_mode & S_IFMT) {
  127. case S_IFREG:
  128. ceph_fscache_register_inode_cookie(inode);
  129. ceph_fscache_file_set_cookie(inode, file);
  130. case S_IFDIR:
  131. dout("init_file %p %p 0%o (regular)\n", inode, file,
  132. inode->i_mode);
  133. cf = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
  134. if (cf == NULL) {
  135. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  136. return -ENOMEM;
  137. }
  138. cf->fmode = fmode;
  139. cf->next_offset = 2;
  140. cf->readdir_cache_idx = -1;
  141. file->private_data = cf;
  142. BUG_ON(inode->i_fop->release != ceph_release);
  143. break;
  144. case S_IFLNK:
  145. dout("init_file %p %p 0%o (symlink)\n", inode, file,
  146. inode->i_mode);
  147. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  148. break;
  149. default:
  150. dout("init_file %p %p 0%o (special)\n", inode, file,
  151. inode->i_mode);
  152. /*
  153. * we need to drop the open ref now, since we don't
  154. * have .release set to ceph_release.
  155. */
  156. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  157. BUG_ON(inode->i_fop->release == ceph_release);
  158. /* call the proper open fop */
  159. ret = inode->i_fop->open(inode, file);
  160. }
  161. return ret;
  162. }
  163. /*
  164. * try renew caps after session gets killed.
  165. */
  166. int ceph_renew_caps(struct inode *inode)
  167. {
  168. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  169. struct ceph_inode_info *ci = ceph_inode(inode);
  170. struct ceph_mds_request *req;
  171. int err, flags, wanted;
  172. spin_lock(&ci->i_ceph_lock);
  173. wanted = __ceph_caps_file_wanted(ci);
  174. if (__ceph_is_any_real_caps(ci) &&
  175. (!(wanted & CEPH_CAP_ANY_WR) == 0 || ci->i_auth_cap)) {
  176. int issued = __ceph_caps_issued(ci, NULL);
  177. spin_unlock(&ci->i_ceph_lock);
  178. dout("renew caps %p want %s issued %s updating mds_wanted\n",
  179. inode, ceph_cap_string(wanted), ceph_cap_string(issued));
  180. ceph_check_caps(ci, 0, NULL);
  181. return 0;
  182. }
  183. spin_unlock(&ci->i_ceph_lock);
  184. flags = 0;
  185. if ((wanted & CEPH_CAP_FILE_RD) && (wanted & CEPH_CAP_FILE_WR))
  186. flags = O_RDWR;
  187. else if (wanted & CEPH_CAP_FILE_RD)
  188. flags = O_RDONLY;
  189. else if (wanted & CEPH_CAP_FILE_WR)
  190. flags = O_WRONLY;
  191. #ifdef O_LAZY
  192. if (wanted & CEPH_CAP_FILE_LAZYIO)
  193. flags |= O_LAZY;
  194. #endif
  195. req = prepare_open_request(inode->i_sb, flags, 0);
  196. if (IS_ERR(req)) {
  197. err = PTR_ERR(req);
  198. goto out;
  199. }
  200. req->r_inode = inode;
  201. ihold(inode);
  202. req->r_num_caps = 1;
  203. req->r_fmode = -1;
  204. err = ceph_mdsc_do_request(mdsc, NULL, req);
  205. ceph_mdsc_put_request(req);
  206. out:
  207. dout("renew caps %p open result=%d\n", inode, err);
  208. return err < 0 ? err : 0;
  209. }
  210. /*
  211. * If we already have the requisite capabilities, we can satisfy
  212. * the open request locally (no need to request new caps from the
  213. * MDS). We do, however, need to inform the MDS (asynchronously)
  214. * if our wanted caps set expands.
  215. */
  216. int ceph_open(struct inode *inode, struct file *file)
  217. {
  218. struct ceph_inode_info *ci = ceph_inode(inode);
  219. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  220. struct ceph_mds_client *mdsc = fsc->mdsc;
  221. struct ceph_mds_request *req;
  222. struct ceph_file_info *cf = file->private_data;
  223. int err;
  224. int flags, fmode, wanted;
  225. if (cf) {
  226. dout("open file %p is already opened\n", file);
  227. return 0;
  228. }
  229. /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
  230. flags = file->f_flags & ~(O_CREAT|O_EXCL);
  231. if (S_ISDIR(inode->i_mode))
  232. flags = O_DIRECTORY; /* mds likes to know */
  233. dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
  234. ceph_vinop(inode), file, flags, file->f_flags);
  235. fmode = ceph_flags_to_mode(flags);
  236. wanted = ceph_caps_for_mode(fmode);
  237. /* snapped files are read-only */
  238. if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
  239. return -EROFS;
  240. /* trivially open snapdir */
  241. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  242. spin_lock(&ci->i_ceph_lock);
  243. __ceph_get_fmode(ci, fmode);
  244. spin_unlock(&ci->i_ceph_lock);
  245. return ceph_init_file(inode, file, fmode);
  246. }
  247. /*
  248. * No need to block if we have caps on the auth MDS (for
  249. * write) or any MDS (for read). Update wanted set
  250. * asynchronously.
  251. */
  252. spin_lock(&ci->i_ceph_lock);
  253. if (__ceph_is_any_real_caps(ci) &&
  254. (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
  255. int mds_wanted = __ceph_caps_mds_wanted(ci);
  256. int issued = __ceph_caps_issued(ci, NULL);
  257. dout("open %p fmode %d want %s issued %s using existing\n",
  258. inode, fmode, ceph_cap_string(wanted),
  259. ceph_cap_string(issued));
  260. __ceph_get_fmode(ci, fmode);
  261. spin_unlock(&ci->i_ceph_lock);
  262. /* adjust wanted? */
  263. if ((issued & wanted) != wanted &&
  264. (mds_wanted & wanted) != wanted &&
  265. ceph_snap(inode) != CEPH_SNAPDIR)
  266. ceph_check_caps(ci, 0, NULL);
  267. return ceph_init_file(inode, file, fmode);
  268. } else if (ceph_snap(inode) != CEPH_NOSNAP &&
  269. (ci->i_snap_caps & wanted) == wanted) {
  270. __ceph_get_fmode(ci, fmode);
  271. spin_unlock(&ci->i_ceph_lock);
  272. return ceph_init_file(inode, file, fmode);
  273. }
  274. spin_unlock(&ci->i_ceph_lock);
  275. dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
  276. req = prepare_open_request(inode->i_sb, flags, 0);
  277. if (IS_ERR(req)) {
  278. err = PTR_ERR(req);
  279. goto out;
  280. }
  281. req->r_inode = inode;
  282. ihold(inode);
  283. req->r_num_caps = 1;
  284. err = ceph_mdsc_do_request(mdsc, NULL, req);
  285. if (!err)
  286. err = ceph_init_file(inode, file, req->r_fmode);
  287. ceph_mdsc_put_request(req);
  288. dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
  289. out:
  290. return err;
  291. }
  292. /*
  293. * Do a lookup + open with a single request. If we get a non-existent
  294. * file or symlink, return 1 so the VFS can retry.
  295. */
  296. int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
  297. struct file *file, unsigned flags, umode_t mode,
  298. int *opened)
  299. {
  300. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  301. struct ceph_mds_client *mdsc = fsc->mdsc;
  302. struct ceph_mds_request *req;
  303. struct dentry *dn;
  304. struct ceph_acls_info acls = {};
  305. int mask;
  306. int err;
  307. dout("atomic_open %p dentry %p '%pd' %s flags %d mode 0%o\n",
  308. dir, dentry, dentry,
  309. d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
  310. if (dentry->d_name.len > NAME_MAX)
  311. return -ENAMETOOLONG;
  312. err = ceph_init_dentry(dentry);
  313. if (err < 0)
  314. return err;
  315. if (flags & O_CREAT) {
  316. err = ceph_pre_init_acls(dir, &mode, &acls);
  317. if (err < 0)
  318. return err;
  319. }
  320. /* do the open */
  321. req = prepare_open_request(dir->i_sb, flags, mode);
  322. if (IS_ERR(req)) {
  323. err = PTR_ERR(req);
  324. goto out_acl;
  325. }
  326. req->r_dentry = dget(dentry);
  327. req->r_num_caps = 2;
  328. if (flags & O_CREAT) {
  329. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  330. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  331. if (acls.pagelist) {
  332. req->r_pagelist = acls.pagelist;
  333. acls.pagelist = NULL;
  334. }
  335. }
  336. mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
  337. if (ceph_security_xattr_wanted(dir))
  338. mask |= CEPH_CAP_XATTR_SHARED;
  339. req->r_args.open.mask = cpu_to_le32(mask);
  340. req->r_locked_dir = dir; /* caller holds dir->i_mutex */
  341. err = ceph_mdsc_do_request(mdsc,
  342. (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
  343. req);
  344. err = ceph_handle_snapdir(req, dentry, err);
  345. if (err)
  346. goto out_req;
  347. if ((flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  348. err = ceph_handle_notrace_create(dir, dentry);
  349. if (d_in_lookup(dentry)) {
  350. dn = ceph_finish_lookup(req, dentry, err);
  351. if (IS_ERR(dn))
  352. err = PTR_ERR(dn);
  353. } else {
  354. /* we were given a hashed negative dentry */
  355. dn = NULL;
  356. }
  357. if (err)
  358. goto out_req;
  359. if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
  360. /* make vfs retry on splice, ENOENT, or symlink */
  361. dout("atomic_open finish_no_open on dn %p\n", dn);
  362. err = finish_no_open(file, dn);
  363. } else {
  364. dout("atomic_open finish_open on dn %p\n", dn);
  365. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  366. ceph_init_inode_acls(d_inode(dentry), &acls);
  367. *opened |= FILE_CREATED;
  368. }
  369. err = finish_open(file, dentry, ceph_open, opened);
  370. }
  371. out_req:
  372. if (!req->r_err && req->r_target_inode)
  373. ceph_put_fmode(ceph_inode(req->r_target_inode), req->r_fmode);
  374. ceph_mdsc_put_request(req);
  375. out_acl:
  376. ceph_release_acls_info(&acls);
  377. dout("atomic_open result=%d\n", err);
  378. return err;
  379. }
  380. int ceph_release(struct inode *inode, struct file *file)
  381. {
  382. struct ceph_inode_info *ci = ceph_inode(inode);
  383. struct ceph_file_info *cf = file->private_data;
  384. dout("release inode %p file %p\n", inode, file);
  385. ceph_put_fmode(ci, cf->fmode);
  386. if (cf->last_readdir)
  387. ceph_mdsc_put_request(cf->last_readdir);
  388. kfree(cf->last_name);
  389. kfree(cf->dir_info);
  390. kmem_cache_free(ceph_file_cachep, cf);
  391. /* wake up anyone waiting for caps on this inode */
  392. wake_up_all(&ci->i_cap_wq);
  393. return 0;
  394. }
  395. enum {
  396. HAVE_RETRIED = 1,
  397. CHECK_EOF = 2,
  398. READ_INLINE = 3,
  399. };
  400. /*
  401. * Read a range of bytes striped over one or more objects. Iterate over
  402. * objects we stripe over. (That's not atomic, but good enough for now.)
  403. *
  404. * If we get a short result from the OSD, check against i_size; we need to
  405. * only return a short read to the caller if we hit EOF.
  406. */
  407. static int striped_read(struct inode *inode,
  408. u64 off, u64 len,
  409. struct page **pages, int num_pages,
  410. int *checkeof)
  411. {
  412. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  413. struct ceph_inode_info *ci = ceph_inode(inode);
  414. u64 pos, this_len, left;
  415. loff_t i_size;
  416. int page_align, pages_left;
  417. int read, ret;
  418. struct page **page_pos;
  419. bool hit_stripe, was_short;
  420. /*
  421. * we may need to do multiple reads. not atomic, unfortunately.
  422. */
  423. pos = off;
  424. left = len;
  425. page_pos = pages;
  426. pages_left = num_pages;
  427. read = 0;
  428. more:
  429. page_align = pos & ~PAGE_MASK;
  430. this_len = left;
  431. ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
  432. &ci->i_layout, pos, &this_len,
  433. ci->i_truncate_seq,
  434. ci->i_truncate_size,
  435. page_pos, pages_left, page_align);
  436. if (ret == -ENOENT)
  437. ret = 0;
  438. hit_stripe = this_len < left;
  439. was_short = ret >= 0 && ret < this_len;
  440. dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, left, read,
  441. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  442. i_size = i_size_read(inode);
  443. if (ret >= 0) {
  444. int didpages;
  445. if (was_short && (pos + ret < i_size)) {
  446. int zlen = min(this_len - ret, i_size - pos - ret);
  447. int zoff = (off & ~PAGE_MASK) + read + ret;
  448. dout(" zero gap %llu to %llu\n",
  449. pos + ret, pos + ret + zlen);
  450. ceph_zero_page_vector_range(zoff, zlen, pages);
  451. ret += zlen;
  452. }
  453. didpages = (page_align + ret) >> PAGE_SHIFT;
  454. pos += ret;
  455. read = pos - off;
  456. left -= ret;
  457. page_pos += didpages;
  458. pages_left -= didpages;
  459. /* hit stripe and need continue*/
  460. if (left && hit_stripe && pos < i_size)
  461. goto more;
  462. }
  463. if (read > 0) {
  464. ret = read;
  465. /* did we bounce off eof? */
  466. if (pos + left > i_size)
  467. *checkeof = CHECK_EOF;
  468. }
  469. dout("striped_read returns %d\n", ret);
  470. return ret;
  471. }
  472. /*
  473. * Completely synchronous read and write methods. Direct from __user
  474. * buffer to osd, or directly to user pages (if O_DIRECT).
  475. *
  476. * If the read spans object boundary, just do multiple reads.
  477. */
  478. static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *i,
  479. int *checkeof)
  480. {
  481. struct file *file = iocb->ki_filp;
  482. struct inode *inode = file_inode(file);
  483. struct page **pages;
  484. u64 off = iocb->ki_pos;
  485. int num_pages, ret;
  486. size_t len = iov_iter_count(i);
  487. dout("sync_read on file %p %llu~%u %s\n", file, off,
  488. (unsigned)len,
  489. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  490. if (!len)
  491. return 0;
  492. /*
  493. * flush any page cache pages in this range. this
  494. * will make concurrent normal and sync io slow,
  495. * but it will at least behave sensibly when they are
  496. * in sequence.
  497. */
  498. ret = filemap_write_and_wait_range(inode->i_mapping, off,
  499. off + len);
  500. if (ret < 0)
  501. return ret;
  502. num_pages = calc_pages_for(off, len);
  503. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  504. if (IS_ERR(pages))
  505. return PTR_ERR(pages);
  506. ret = striped_read(inode, off, len, pages,
  507. num_pages, checkeof);
  508. if (ret > 0) {
  509. int l, k = 0;
  510. size_t left = ret;
  511. while (left) {
  512. size_t page_off = off & ~PAGE_MASK;
  513. size_t copy = min_t(size_t, left,
  514. PAGE_SIZE - page_off);
  515. l = copy_page_to_iter(pages[k++], page_off, copy, i);
  516. off += l;
  517. left -= l;
  518. if (l < copy)
  519. break;
  520. }
  521. }
  522. ceph_release_page_vector(pages, num_pages);
  523. if (off > iocb->ki_pos) {
  524. ret = off - iocb->ki_pos;
  525. iocb->ki_pos = off;
  526. }
  527. dout("sync_read result %d\n", ret);
  528. return ret;
  529. }
  530. struct ceph_aio_request {
  531. struct kiocb *iocb;
  532. size_t total_len;
  533. int write;
  534. int error;
  535. struct list_head osd_reqs;
  536. unsigned num_reqs;
  537. atomic_t pending_reqs;
  538. struct timespec mtime;
  539. struct ceph_cap_flush *prealloc_cf;
  540. };
  541. struct ceph_aio_work {
  542. struct work_struct work;
  543. struct ceph_osd_request *req;
  544. };
  545. static void ceph_aio_retry_work(struct work_struct *work);
  546. static void ceph_aio_complete(struct inode *inode,
  547. struct ceph_aio_request *aio_req)
  548. {
  549. struct ceph_inode_info *ci = ceph_inode(inode);
  550. int ret;
  551. if (!atomic_dec_and_test(&aio_req->pending_reqs))
  552. return;
  553. ret = aio_req->error;
  554. if (!ret)
  555. ret = aio_req->total_len;
  556. dout("ceph_aio_complete %p rc %d\n", inode, ret);
  557. if (ret >= 0 && aio_req->write) {
  558. int dirty;
  559. loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
  560. if (endoff > i_size_read(inode)) {
  561. if (ceph_inode_set_size(inode, endoff))
  562. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  563. }
  564. spin_lock(&ci->i_ceph_lock);
  565. ci->i_inline_version = CEPH_INLINE_NONE;
  566. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  567. &aio_req->prealloc_cf);
  568. spin_unlock(&ci->i_ceph_lock);
  569. if (dirty)
  570. __mark_inode_dirty(inode, dirty);
  571. }
  572. ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
  573. CEPH_CAP_FILE_RD));
  574. aio_req->iocb->ki_complete(aio_req->iocb, ret, 0);
  575. ceph_free_cap_flush(aio_req->prealloc_cf);
  576. kfree(aio_req);
  577. }
  578. static void ceph_aio_complete_req(struct ceph_osd_request *req)
  579. {
  580. int rc = req->r_result;
  581. struct inode *inode = req->r_inode;
  582. struct ceph_aio_request *aio_req = req->r_priv;
  583. struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
  584. int num_pages = calc_pages_for((u64)osd_data->alignment,
  585. osd_data->length);
  586. dout("ceph_aio_complete_req %p rc %d bytes %llu\n",
  587. inode, rc, osd_data->length);
  588. if (rc == -EOLDSNAPC) {
  589. struct ceph_aio_work *aio_work;
  590. BUG_ON(!aio_req->write);
  591. aio_work = kmalloc(sizeof(*aio_work), GFP_NOFS);
  592. if (aio_work) {
  593. INIT_WORK(&aio_work->work, ceph_aio_retry_work);
  594. aio_work->req = req;
  595. queue_work(ceph_inode_to_client(inode)->wb_wq,
  596. &aio_work->work);
  597. return;
  598. }
  599. rc = -ENOMEM;
  600. } else if (!aio_req->write) {
  601. if (rc == -ENOENT)
  602. rc = 0;
  603. if (rc >= 0 && osd_data->length > rc) {
  604. int zoff = osd_data->alignment + rc;
  605. int zlen = osd_data->length - rc;
  606. /*
  607. * If read is satisfied by single OSD request,
  608. * it can pass EOF. Otherwise read is within
  609. * i_size.
  610. */
  611. if (aio_req->num_reqs == 1) {
  612. loff_t i_size = i_size_read(inode);
  613. loff_t endoff = aio_req->iocb->ki_pos + rc;
  614. if (endoff < i_size)
  615. zlen = min_t(size_t, zlen,
  616. i_size - endoff);
  617. aio_req->total_len = rc + zlen;
  618. }
  619. if (zlen > 0)
  620. ceph_zero_page_vector_range(zoff, zlen,
  621. osd_data->pages);
  622. }
  623. }
  624. ceph_put_page_vector(osd_data->pages, num_pages, !aio_req->write);
  625. ceph_osdc_put_request(req);
  626. if (rc < 0)
  627. cmpxchg(&aio_req->error, 0, rc);
  628. ceph_aio_complete(inode, aio_req);
  629. return;
  630. }
  631. static void ceph_aio_retry_work(struct work_struct *work)
  632. {
  633. struct ceph_aio_work *aio_work =
  634. container_of(work, struct ceph_aio_work, work);
  635. struct ceph_osd_request *orig_req = aio_work->req;
  636. struct ceph_aio_request *aio_req = orig_req->r_priv;
  637. struct inode *inode = orig_req->r_inode;
  638. struct ceph_inode_info *ci = ceph_inode(inode);
  639. struct ceph_snap_context *snapc;
  640. struct ceph_osd_request *req;
  641. int ret;
  642. spin_lock(&ci->i_ceph_lock);
  643. if (__ceph_have_pending_cap_snap(ci)) {
  644. struct ceph_cap_snap *capsnap =
  645. list_last_entry(&ci->i_cap_snaps,
  646. struct ceph_cap_snap,
  647. ci_item);
  648. snapc = ceph_get_snap_context(capsnap->context);
  649. } else {
  650. BUG_ON(!ci->i_head_snapc);
  651. snapc = ceph_get_snap_context(ci->i_head_snapc);
  652. }
  653. spin_unlock(&ci->i_ceph_lock);
  654. req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 2,
  655. false, GFP_NOFS);
  656. if (!req) {
  657. ret = -ENOMEM;
  658. req = orig_req;
  659. goto out;
  660. }
  661. req->r_flags = CEPH_OSD_FLAG_ORDERSNAP |
  662. CEPH_OSD_FLAG_ONDISK |
  663. CEPH_OSD_FLAG_WRITE;
  664. ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
  665. ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
  666. ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
  667. if (ret) {
  668. ceph_osdc_put_request(req);
  669. req = orig_req;
  670. goto out;
  671. }
  672. req->r_ops[0] = orig_req->r_ops[0];
  673. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC, 0);
  674. req->r_mtime = aio_req->mtime;
  675. req->r_data_offset = req->r_ops[0].extent.offset;
  676. ceph_osdc_put_request(orig_req);
  677. req->r_callback = ceph_aio_complete_req;
  678. req->r_inode = inode;
  679. req->r_priv = aio_req;
  680. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  681. out:
  682. if (ret < 0) {
  683. req->r_result = ret;
  684. ceph_aio_complete_req(req);
  685. }
  686. ceph_put_snap_context(snapc);
  687. kfree(aio_work);
  688. }
  689. /*
  690. * Write commit request unsafe callback, called to tell us when a
  691. * request is unsafe (that is, in flight--has been handed to the
  692. * messenger to send to its target osd). It is called again when
  693. * we've received a response message indicating the request is
  694. * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
  695. * is completed early (and unsuccessfully) due to a timeout or
  696. * interrupt.
  697. *
  698. * This is used if we requested both an ACK and ONDISK commit reply
  699. * from the OSD.
  700. */
  701. static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
  702. {
  703. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  704. dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
  705. unsafe ? "un" : "");
  706. if (unsafe) {
  707. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  708. spin_lock(&ci->i_unsafe_lock);
  709. list_add_tail(&req->r_unsafe_item,
  710. &ci->i_unsafe_writes);
  711. spin_unlock(&ci->i_unsafe_lock);
  712. complete_all(&req->r_completion);
  713. } else {
  714. spin_lock(&ci->i_unsafe_lock);
  715. list_del_init(&req->r_unsafe_item);
  716. spin_unlock(&ci->i_unsafe_lock);
  717. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  718. }
  719. }
  720. /*
  721. * Wait on any unsafe replies for the given inode. First wait on the
  722. * newest request, and make that the upper bound. Then, if there are
  723. * more requests, keep waiting on the oldest as long as it is still older
  724. * than the original request.
  725. */
  726. void ceph_sync_write_wait(struct inode *inode)
  727. {
  728. struct ceph_inode_info *ci = ceph_inode(inode);
  729. struct list_head *head = &ci->i_unsafe_writes;
  730. struct ceph_osd_request *req;
  731. u64 last_tid;
  732. if (!S_ISREG(inode->i_mode))
  733. return;
  734. spin_lock(&ci->i_unsafe_lock);
  735. if (list_empty(head))
  736. goto out;
  737. /* set upper bound as _last_ entry in chain */
  738. req = list_last_entry(head, struct ceph_osd_request,
  739. r_unsafe_item);
  740. last_tid = req->r_tid;
  741. do {
  742. ceph_osdc_get_request(req);
  743. spin_unlock(&ci->i_unsafe_lock);
  744. dout("sync_write_wait on tid %llu (until %llu)\n",
  745. req->r_tid, last_tid);
  746. wait_for_completion(&req->r_safe_completion);
  747. ceph_osdc_put_request(req);
  748. spin_lock(&ci->i_unsafe_lock);
  749. /*
  750. * from here on look at first entry in chain, since we
  751. * only want to wait for anything older than last_tid
  752. */
  753. if (list_empty(head))
  754. break;
  755. req = list_first_entry(head, struct ceph_osd_request,
  756. r_unsafe_item);
  757. } while (req->r_tid < last_tid);
  758. out:
  759. spin_unlock(&ci->i_unsafe_lock);
  760. }
  761. static ssize_t
  762. ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
  763. struct ceph_snap_context *snapc,
  764. struct ceph_cap_flush **pcf)
  765. {
  766. struct file *file = iocb->ki_filp;
  767. struct inode *inode = file_inode(file);
  768. struct ceph_inode_info *ci = ceph_inode(inode);
  769. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  770. struct ceph_vino vino;
  771. struct ceph_osd_request *req;
  772. struct page **pages;
  773. struct ceph_aio_request *aio_req = NULL;
  774. int num_pages = 0;
  775. int flags;
  776. int ret;
  777. struct timespec mtime = current_fs_time(inode->i_sb);
  778. size_t count = iov_iter_count(iter);
  779. loff_t pos = iocb->ki_pos;
  780. bool write = iov_iter_rw(iter) == WRITE;
  781. if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  782. return -EROFS;
  783. dout("sync_direct_read_write (%s) on file %p %lld~%u\n",
  784. (write ? "write" : "read"), file, pos, (unsigned)count);
  785. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  786. if (ret < 0)
  787. return ret;
  788. if (write) {
  789. ret = invalidate_inode_pages2_range(inode->i_mapping,
  790. pos >> PAGE_SHIFT,
  791. (pos + count) >> PAGE_SHIFT);
  792. if (ret < 0)
  793. dout("invalidate_inode_pages2_range returned %d\n", ret);
  794. flags = CEPH_OSD_FLAG_ORDERSNAP |
  795. CEPH_OSD_FLAG_ONDISK |
  796. CEPH_OSD_FLAG_WRITE;
  797. } else {
  798. flags = CEPH_OSD_FLAG_READ;
  799. }
  800. while (iov_iter_count(iter) > 0) {
  801. u64 size = dio_get_pagev_size(iter);
  802. size_t start = 0;
  803. ssize_t len;
  804. vino = ceph_vino(inode);
  805. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  806. vino, pos, &size, 0,
  807. /*include a 'startsync' command*/
  808. write ? 2 : 1,
  809. write ? CEPH_OSD_OP_WRITE :
  810. CEPH_OSD_OP_READ,
  811. flags, snapc,
  812. ci->i_truncate_seq,
  813. ci->i_truncate_size,
  814. false);
  815. if (IS_ERR(req)) {
  816. ret = PTR_ERR(req);
  817. break;
  818. }
  819. len = size;
  820. pages = dio_get_pages_alloc(iter, len, &start, &num_pages);
  821. if (IS_ERR(pages)) {
  822. ceph_osdc_put_request(req);
  823. ret = PTR_ERR(pages);
  824. break;
  825. }
  826. /*
  827. * To simplify error handling, allow AIO when IO within i_size
  828. * or IO can be satisfied by single OSD request.
  829. */
  830. if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
  831. (len == count || pos + count <= i_size_read(inode))) {
  832. aio_req = kzalloc(sizeof(*aio_req), GFP_KERNEL);
  833. if (aio_req) {
  834. aio_req->iocb = iocb;
  835. aio_req->write = write;
  836. INIT_LIST_HEAD(&aio_req->osd_reqs);
  837. if (write) {
  838. aio_req->mtime = mtime;
  839. swap(aio_req->prealloc_cf, *pcf);
  840. }
  841. }
  842. /* ignore error */
  843. }
  844. if (write) {
  845. /*
  846. * throw out any page cache pages in this range. this
  847. * may block.
  848. */
  849. truncate_inode_pages_range(inode->i_mapping, pos,
  850. (pos+len) | (PAGE_SIZE - 1));
  851. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC, 0);
  852. req->r_mtime = mtime;
  853. }
  854. osd_req_op_extent_osd_data_pages(req, 0, pages, len, start,
  855. false, false);
  856. if (aio_req) {
  857. aio_req->total_len += len;
  858. aio_req->num_reqs++;
  859. atomic_inc(&aio_req->pending_reqs);
  860. req->r_callback = ceph_aio_complete_req;
  861. req->r_inode = inode;
  862. req->r_priv = aio_req;
  863. list_add_tail(&req->r_unsafe_item, &aio_req->osd_reqs);
  864. pos += len;
  865. iov_iter_advance(iter, len);
  866. continue;
  867. }
  868. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  869. if (!ret)
  870. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  871. size = i_size_read(inode);
  872. if (!write) {
  873. if (ret == -ENOENT)
  874. ret = 0;
  875. if (ret >= 0 && ret < len && pos + ret < size) {
  876. int zlen = min_t(size_t, len - ret,
  877. size - pos - ret);
  878. ceph_zero_page_vector_range(start + ret, zlen,
  879. pages);
  880. ret += zlen;
  881. }
  882. if (ret >= 0)
  883. len = ret;
  884. }
  885. ceph_put_page_vector(pages, num_pages, !write);
  886. ceph_osdc_put_request(req);
  887. if (ret < 0)
  888. break;
  889. pos += len;
  890. iov_iter_advance(iter, len);
  891. if (!write && pos >= size)
  892. break;
  893. if (write && pos > size) {
  894. if (ceph_inode_set_size(inode, pos))
  895. ceph_check_caps(ceph_inode(inode),
  896. CHECK_CAPS_AUTHONLY,
  897. NULL);
  898. }
  899. }
  900. if (aio_req) {
  901. LIST_HEAD(osd_reqs);
  902. if (aio_req->num_reqs == 0) {
  903. kfree(aio_req);
  904. return ret;
  905. }
  906. ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
  907. CEPH_CAP_FILE_RD);
  908. list_splice(&aio_req->osd_reqs, &osd_reqs);
  909. while (!list_empty(&osd_reqs)) {
  910. req = list_first_entry(&osd_reqs,
  911. struct ceph_osd_request,
  912. r_unsafe_item);
  913. list_del_init(&req->r_unsafe_item);
  914. if (ret >= 0)
  915. ret = ceph_osdc_start_request(req->r_osdc,
  916. req, false);
  917. if (ret < 0) {
  918. req->r_result = ret;
  919. ceph_aio_complete_req(req);
  920. }
  921. }
  922. return -EIOCBQUEUED;
  923. }
  924. if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
  925. ret = pos - iocb->ki_pos;
  926. iocb->ki_pos = pos;
  927. }
  928. return ret;
  929. }
  930. /*
  931. * Synchronous write, straight from __user pointer or user pages.
  932. *
  933. * If write spans object boundary, just do multiple writes. (For a
  934. * correct atomic write, we should e.g. take write locks on all
  935. * objects, rollback on failure, etc.)
  936. */
  937. static ssize_t
  938. ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
  939. struct ceph_snap_context *snapc)
  940. {
  941. struct file *file = iocb->ki_filp;
  942. struct inode *inode = file_inode(file);
  943. struct ceph_inode_info *ci = ceph_inode(inode);
  944. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  945. struct ceph_vino vino;
  946. struct ceph_osd_request *req;
  947. struct page **pages;
  948. u64 len;
  949. int num_pages;
  950. int written = 0;
  951. int flags;
  952. int check_caps = 0;
  953. int ret;
  954. struct timespec mtime = current_fs_time(inode->i_sb);
  955. size_t count = iov_iter_count(from);
  956. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  957. return -EROFS;
  958. dout("sync_write on file %p %lld~%u\n", file, pos, (unsigned)count);
  959. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  960. if (ret < 0)
  961. return ret;
  962. ret = invalidate_inode_pages2_range(inode->i_mapping,
  963. pos >> PAGE_SHIFT,
  964. (pos + count) >> PAGE_SHIFT);
  965. if (ret < 0)
  966. dout("invalidate_inode_pages2_range returned %d\n", ret);
  967. flags = CEPH_OSD_FLAG_ORDERSNAP |
  968. CEPH_OSD_FLAG_ONDISK |
  969. CEPH_OSD_FLAG_WRITE |
  970. CEPH_OSD_FLAG_ACK;
  971. while ((len = iov_iter_count(from)) > 0) {
  972. size_t left;
  973. int n;
  974. vino = ceph_vino(inode);
  975. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  976. vino, pos, &len, 0, 1,
  977. CEPH_OSD_OP_WRITE, flags, snapc,
  978. ci->i_truncate_seq,
  979. ci->i_truncate_size,
  980. false);
  981. if (IS_ERR(req)) {
  982. ret = PTR_ERR(req);
  983. break;
  984. }
  985. /*
  986. * write from beginning of first page,
  987. * regardless of io alignment
  988. */
  989. num_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  990. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  991. if (IS_ERR(pages)) {
  992. ret = PTR_ERR(pages);
  993. goto out;
  994. }
  995. left = len;
  996. for (n = 0; n < num_pages; n++) {
  997. size_t plen = min_t(size_t, left, PAGE_SIZE);
  998. ret = copy_page_from_iter(pages[n], 0, plen, from);
  999. if (ret != plen) {
  1000. ret = -EFAULT;
  1001. break;
  1002. }
  1003. left -= ret;
  1004. }
  1005. if (ret < 0) {
  1006. ceph_release_page_vector(pages, num_pages);
  1007. goto out;
  1008. }
  1009. /* get a second commit callback */
  1010. req->r_unsafe_callback = ceph_sync_write_unsafe;
  1011. req->r_inode = inode;
  1012. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
  1013. false, true);
  1014. req->r_mtime = mtime;
  1015. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1016. if (!ret)
  1017. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1018. out:
  1019. ceph_osdc_put_request(req);
  1020. if (ret == 0) {
  1021. pos += len;
  1022. written += len;
  1023. if (pos > i_size_read(inode)) {
  1024. check_caps = ceph_inode_set_size(inode, pos);
  1025. if (check_caps)
  1026. ceph_check_caps(ceph_inode(inode),
  1027. CHECK_CAPS_AUTHONLY,
  1028. NULL);
  1029. }
  1030. } else
  1031. break;
  1032. }
  1033. if (ret != -EOLDSNAPC && written > 0) {
  1034. ret = written;
  1035. iocb->ki_pos = pos;
  1036. }
  1037. return ret;
  1038. }
  1039. /*
  1040. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  1041. * Atomically grab references, so that those bits are not released
  1042. * back to the MDS mid-read.
  1043. *
  1044. * Hmm, the sync read case isn't actually async... should it be?
  1045. */
  1046. static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1047. {
  1048. struct file *filp = iocb->ki_filp;
  1049. struct ceph_file_info *fi = filp->private_data;
  1050. size_t len = iov_iter_count(to);
  1051. struct inode *inode = file_inode(filp);
  1052. struct ceph_inode_info *ci = ceph_inode(inode);
  1053. struct page *pinned_page = NULL;
  1054. ssize_t ret;
  1055. int want, got = 0;
  1056. int retry_op = 0, read = 0;
  1057. again:
  1058. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  1059. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
  1060. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1061. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  1062. else
  1063. want = CEPH_CAP_FILE_CACHE;
  1064. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
  1065. if (ret < 0)
  1066. return ret;
  1067. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  1068. (iocb->ki_flags & IOCB_DIRECT) ||
  1069. (fi->flags & CEPH_F_SYNC)) {
  1070. dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  1071. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1072. ceph_cap_string(got));
  1073. if (ci->i_inline_version == CEPH_INLINE_NONE) {
  1074. if (!retry_op && (iocb->ki_flags & IOCB_DIRECT)) {
  1075. ret = ceph_direct_read_write(iocb, to,
  1076. NULL, NULL);
  1077. if (ret >= 0 && ret < len)
  1078. retry_op = CHECK_EOF;
  1079. } else {
  1080. ret = ceph_sync_read(iocb, to, &retry_op);
  1081. }
  1082. } else {
  1083. retry_op = READ_INLINE;
  1084. }
  1085. } else {
  1086. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  1087. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1088. ceph_cap_string(got));
  1089. ret = generic_file_read_iter(iocb, to);
  1090. }
  1091. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  1092. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  1093. if (pinned_page) {
  1094. put_page(pinned_page);
  1095. pinned_page = NULL;
  1096. }
  1097. ceph_put_cap_refs(ci, got);
  1098. if (retry_op > HAVE_RETRIED && ret >= 0) {
  1099. int statret;
  1100. struct page *page = NULL;
  1101. loff_t i_size;
  1102. if (retry_op == READ_INLINE) {
  1103. page = __page_cache_alloc(GFP_KERNEL);
  1104. if (!page)
  1105. return -ENOMEM;
  1106. }
  1107. statret = __ceph_do_getattr(inode, page,
  1108. CEPH_STAT_CAP_INLINE_DATA, !!page);
  1109. if (statret < 0) {
  1110. __free_page(page);
  1111. if (statret == -ENODATA) {
  1112. BUG_ON(retry_op != READ_INLINE);
  1113. goto again;
  1114. }
  1115. return statret;
  1116. }
  1117. i_size = i_size_read(inode);
  1118. if (retry_op == READ_INLINE) {
  1119. BUG_ON(ret > 0 || read > 0);
  1120. if (iocb->ki_pos < i_size &&
  1121. iocb->ki_pos < PAGE_SIZE) {
  1122. loff_t end = min_t(loff_t, i_size,
  1123. iocb->ki_pos + len);
  1124. end = min_t(loff_t, end, PAGE_SIZE);
  1125. if (statret < end)
  1126. zero_user_segment(page, statret, end);
  1127. ret = copy_page_to_iter(page,
  1128. iocb->ki_pos & ~PAGE_MASK,
  1129. end - iocb->ki_pos, to);
  1130. iocb->ki_pos += ret;
  1131. read += ret;
  1132. }
  1133. if (iocb->ki_pos < i_size && read < len) {
  1134. size_t zlen = min_t(size_t, len - read,
  1135. i_size - iocb->ki_pos);
  1136. ret = iov_iter_zero(zlen, to);
  1137. iocb->ki_pos += ret;
  1138. read += ret;
  1139. }
  1140. __free_pages(page, 0);
  1141. return read;
  1142. }
  1143. /* hit EOF or hole? */
  1144. if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
  1145. ret < len) {
  1146. dout("sync_read hit hole, ppos %lld < size %lld"
  1147. ", reading more\n", iocb->ki_pos, i_size);
  1148. read += ret;
  1149. len -= ret;
  1150. retry_op = HAVE_RETRIED;
  1151. goto again;
  1152. }
  1153. }
  1154. if (ret >= 0)
  1155. ret += read;
  1156. return ret;
  1157. }
  1158. /*
  1159. * Take cap references to avoid releasing caps to MDS mid-write.
  1160. *
  1161. * If we are synchronous, and write with an old snap context, the OSD
  1162. * may return EOLDSNAPC. In that case, retry the write.. _after_
  1163. * dropping our cap refs and allowing the pending snap to logically
  1164. * complete _before_ this write occurs.
  1165. *
  1166. * If we are near ENOSPC, write synchronously.
  1167. */
  1168. static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1169. {
  1170. struct file *file = iocb->ki_filp;
  1171. struct ceph_file_info *fi = file->private_data;
  1172. struct inode *inode = file_inode(file);
  1173. struct ceph_inode_info *ci = ceph_inode(inode);
  1174. struct ceph_osd_client *osdc =
  1175. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  1176. struct ceph_cap_flush *prealloc_cf;
  1177. ssize_t count, written = 0;
  1178. int err, want, got;
  1179. loff_t pos;
  1180. if (ceph_snap(inode) != CEPH_NOSNAP)
  1181. return -EROFS;
  1182. prealloc_cf = ceph_alloc_cap_flush();
  1183. if (!prealloc_cf)
  1184. return -ENOMEM;
  1185. inode_lock(inode);
  1186. /* We can write back this queue in page reclaim */
  1187. current->backing_dev_info = inode_to_bdi(inode);
  1188. if (iocb->ki_flags & IOCB_APPEND) {
  1189. err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1190. if (err < 0)
  1191. goto out;
  1192. }
  1193. err = generic_write_checks(iocb, from);
  1194. if (err <= 0)
  1195. goto out;
  1196. pos = iocb->ki_pos;
  1197. count = iov_iter_count(from);
  1198. err = file_remove_privs(file);
  1199. if (err)
  1200. goto out;
  1201. err = file_update_time(file);
  1202. if (err)
  1203. goto out;
  1204. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1205. err = ceph_uninline_data(file, NULL);
  1206. if (err < 0)
  1207. goto out;
  1208. }
  1209. retry_snap:
  1210. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL)) {
  1211. err = -ENOSPC;
  1212. goto out;
  1213. }
  1214. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  1215. inode, ceph_vinop(inode), pos, count, i_size_read(inode));
  1216. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1217. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1218. else
  1219. want = CEPH_CAP_FILE_BUFFER;
  1220. got = 0;
  1221. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, pos + count,
  1222. &got, NULL);
  1223. if (err < 0)
  1224. goto out;
  1225. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  1226. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  1227. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  1228. (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC)) {
  1229. struct ceph_snap_context *snapc;
  1230. struct iov_iter data;
  1231. inode_unlock(inode);
  1232. spin_lock(&ci->i_ceph_lock);
  1233. if (__ceph_have_pending_cap_snap(ci)) {
  1234. struct ceph_cap_snap *capsnap =
  1235. list_last_entry(&ci->i_cap_snaps,
  1236. struct ceph_cap_snap,
  1237. ci_item);
  1238. snapc = ceph_get_snap_context(capsnap->context);
  1239. } else {
  1240. BUG_ON(!ci->i_head_snapc);
  1241. snapc = ceph_get_snap_context(ci->i_head_snapc);
  1242. }
  1243. spin_unlock(&ci->i_ceph_lock);
  1244. /* we might need to revert back to that point */
  1245. data = *from;
  1246. if (iocb->ki_flags & IOCB_DIRECT)
  1247. written = ceph_direct_read_write(iocb, &data, snapc,
  1248. &prealloc_cf);
  1249. else
  1250. written = ceph_sync_write(iocb, &data, pos, snapc);
  1251. if (written == -EOLDSNAPC) {
  1252. dout("aio_write %p %llx.%llx %llu~%u"
  1253. "got EOLDSNAPC, retrying\n",
  1254. inode, ceph_vinop(inode),
  1255. pos, (unsigned)count);
  1256. inode_lock(inode);
  1257. goto retry_snap;
  1258. }
  1259. if (written > 0)
  1260. iov_iter_advance(from, written);
  1261. ceph_put_snap_context(snapc);
  1262. } else {
  1263. /*
  1264. * No need to acquire the i_truncate_mutex. Because
  1265. * the MDS revokes Fwb caps before sending truncate
  1266. * message to us. We can't get Fwb cap while there
  1267. * are pending vmtruncate. So write and vmtruncate
  1268. * can not run at the same time
  1269. */
  1270. written = generic_perform_write(file, from, pos);
  1271. if (likely(written >= 0))
  1272. iocb->ki_pos = pos + written;
  1273. inode_unlock(inode);
  1274. }
  1275. if (written >= 0) {
  1276. int dirty;
  1277. spin_lock(&ci->i_ceph_lock);
  1278. ci->i_inline_version = CEPH_INLINE_NONE;
  1279. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1280. &prealloc_cf);
  1281. spin_unlock(&ci->i_ceph_lock);
  1282. if (dirty)
  1283. __mark_inode_dirty(inode, dirty);
  1284. }
  1285. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  1286. inode, ceph_vinop(inode), pos, (unsigned)count,
  1287. ceph_cap_string(got));
  1288. ceph_put_cap_refs(ci, got);
  1289. if (written >= 0) {
  1290. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_NEARFULL))
  1291. iocb->ki_flags |= IOCB_DSYNC;
  1292. written = generic_write_sync(iocb, written);
  1293. }
  1294. goto out_unlocked;
  1295. out:
  1296. inode_unlock(inode);
  1297. out_unlocked:
  1298. ceph_free_cap_flush(prealloc_cf);
  1299. current->backing_dev_info = NULL;
  1300. return written ? written : err;
  1301. }
  1302. /*
  1303. * llseek. be sure to verify file size on SEEK_END.
  1304. */
  1305. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  1306. {
  1307. struct inode *inode = file->f_mapping->host;
  1308. loff_t i_size;
  1309. loff_t ret;
  1310. inode_lock(inode);
  1311. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  1312. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1313. if (ret < 0)
  1314. goto out;
  1315. }
  1316. i_size = i_size_read(inode);
  1317. switch (whence) {
  1318. case SEEK_END:
  1319. offset += i_size;
  1320. break;
  1321. case SEEK_CUR:
  1322. /*
  1323. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  1324. * position-querying operation. Avoid rewriting the "same"
  1325. * f_pos value back to the file because a concurrent read(),
  1326. * write() or lseek() might have altered it
  1327. */
  1328. if (offset == 0) {
  1329. ret = file->f_pos;
  1330. goto out;
  1331. }
  1332. offset += file->f_pos;
  1333. break;
  1334. case SEEK_DATA:
  1335. if (offset >= i_size) {
  1336. ret = -ENXIO;
  1337. goto out;
  1338. }
  1339. break;
  1340. case SEEK_HOLE:
  1341. if (offset >= i_size) {
  1342. ret = -ENXIO;
  1343. goto out;
  1344. }
  1345. offset = i_size;
  1346. break;
  1347. }
  1348. ret = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  1349. out:
  1350. inode_unlock(inode);
  1351. return ret;
  1352. }
  1353. static inline void ceph_zero_partial_page(
  1354. struct inode *inode, loff_t offset, unsigned size)
  1355. {
  1356. struct page *page;
  1357. pgoff_t index = offset >> PAGE_SHIFT;
  1358. page = find_lock_page(inode->i_mapping, index);
  1359. if (page) {
  1360. wait_on_page_writeback(page);
  1361. zero_user(page, offset & (PAGE_SIZE - 1), size);
  1362. unlock_page(page);
  1363. put_page(page);
  1364. }
  1365. }
  1366. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  1367. loff_t length)
  1368. {
  1369. loff_t nearly = round_up(offset, PAGE_SIZE);
  1370. if (offset < nearly) {
  1371. loff_t size = nearly - offset;
  1372. if (length < size)
  1373. size = length;
  1374. ceph_zero_partial_page(inode, offset, size);
  1375. offset += size;
  1376. length -= size;
  1377. }
  1378. if (length >= PAGE_SIZE) {
  1379. loff_t size = round_down(length, PAGE_SIZE);
  1380. truncate_pagecache_range(inode, offset, offset + size - 1);
  1381. offset += size;
  1382. length -= size;
  1383. }
  1384. if (length)
  1385. ceph_zero_partial_page(inode, offset, length);
  1386. }
  1387. static int ceph_zero_partial_object(struct inode *inode,
  1388. loff_t offset, loff_t *length)
  1389. {
  1390. struct ceph_inode_info *ci = ceph_inode(inode);
  1391. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1392. struct ceph_osd_request *req;
  1393. int ret = 0;
  1394. loff_t zero = 0;
  1395. int op;
  1396. if (!length) {
  1397. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  1398. length = &zero;
  1399. } else {
  1400. op = CEPH_OSD_OP_ZERO;
  1401. }
  1402. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1403. ceph_vino(inode),
  1404. offset, length,
  1405. 0, 1, op,
  1406. CEPH_OSD_FLAG_WRITE |
  1407. CEPH_OSD_FLAG_ONDISK,
  1408. NULL, 0, 0, false);
  1409. if (IS_ERR(req)) {
  1410. ret = PTR_ERR(req);
  1411. goto out;
  1412. }
  1413. req->r_mtime = inode->i_mtime;
  1414. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1415. if (!ret) {
  1416. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1417. if (ret == -ENOENT)
  1418. ret = 0;
  1419. }
  1420. ceph_osdc_put_request(req);
  1421. out:
  1422. return ret;
  1423. }
  1424. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  1425. {
  1426. int ret = 0;
  1427. struct ceph_inode_info *ci = ceph_inode(inode);
  1428. s32 stripe_unit = ci->i_layout.stripe_unit;
  1429. s32 stripe_count = ci->i_layout.stripe_count;
  1430. s32 object_size = ci->i_layout.object_size;
  1431. u64 object_set_size = object_size * stripe_count;
  1432. u64 nearly, t;
  1433. /* round offset up to next period boundary */
  1434. nearly = offset + object_set_size - 1;
  1435. t = nearly;
  1436. nearly -= do_div(t, object_set_size);
  1437. while (length && offset < nearly) {
  1438. loff_t size = length;
  1439. ret = ceph_zero_partial_object(inode, offset, &size);
  1440. if (ret < 0)
  1441. return ret;
  1442. offset += size;
  1443. length -= size;
  1444. }
  1445. while (length >= object_set_size) {
  1446. int i;
  1447. loff_t pos = offset;
  1448. for (i = 0; i < stripe_count; ++i) {
  1449. ret = ceph_zero_partial_object(inode, pos, NULL);
  1450. if (ret < 0)
  1451. return ret;
  1452. pos += stripe_unit;
  1453. }
  1454. offset += object_set_size;
  1455. length -= object_set_size;
  1456. }
  1457. while (length) {
  1458. loff_t size = length;
  1459. ret = ceph_zero_partial_object(inode, offset, &size);
  1460. if (ret < 0)
  1461. return ret;
  1462. offset += size;
  1463. length -= size;
  1464. }
  1465. return ret;
  1466. }
  1467. static long ceph_fallocate(struct file *file, int mode,
  1468. loff_t offset, loff_t length)
  1469. {
  1470. struct ceph_file_info *fi = file->private_data;
  1471. struct inode *inode = file_inode(file);
  1472. struct ceph_inode_info *ci = ceph_inode(inode);
  1473. struct ceph_osd_client *osdc =
  1474. &ceph_inode_to_client(inode)->client->osdc;
  1475. struct ceph_cap_flush *prealloc_cf;
  1476. int want, got = 0;
  1477. int dirty;
  1478. int ret = 0;
  1479. loff_t endoff = 0;
  1480. loff_t size;
  1481. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1482. return -EOPNOTSUPP;
  1483. if (!S_ISREG(inode->i_mode))
  1484. return -EOPNOTSUPP;
  1485. prealloc_cf = ceph_alloc_cap_flush();
  1486. if (!prealloc_cf)
  1487. return -ENOMEM;
  1488. inode_lock(inode);
  1489. if (ceph_snap(inode) != CEPH_NOSNAP) {
  1490. ret = -EROFS;
  1491. goto unlock;
  1492. }
  1493. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) &&
  1494. !(mode & FALLOC_FL_PUNCH_HOLE)) {
  1495. ret = -ENOSPC;
  1496. goto unlock;
  1497. }
  1498. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1499. ret = ceph_uninline_data(file, NULL);
  1500. if (ret < 0)
  1501. goto unlock;
  1502. }
  1503. size = i_size_read(inode);
  1504. if (!(mode & FALLOC_FL_KEEP_SIZE))
  1505. endoff = offset + length;
  1506. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1507. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1508. else
  1509. want = CEPH_CAP_FILE_BUFFER;
  1510. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, endoff, &got, NULL);
  1511. if (ret < 0)
  1512. goto unlock;
  1513. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1514. if (offset < size)
  1515. ceph_zero_pagecache_range(inode, offset, length);
  1516. ret = ceph_zero_objects(inode, offset, length);
  1517. } else if (endoff > size) {
  1518. truncate_pagecache_range(inode, size, -1);
  1519. if (ceph_inode_set_size(inode, endoff))
  1520. ceph_check_caps(ceph_inode(inode),
  1521. CHECK_CAPS_AUTHONLY, NULL);
  1522. }
  1523. if (!ret) {
  1524. spin_lock(&ci->i_ceph_lock);
  1525. ci->i_inline_version = CEPH_INLINE_NONE;
  1526. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1527. &prealloc_cf);
  1528. spin_unlock(&ci->i_ceph_lock);
  1529. if (dirty)
  1530. __mark_inode_dirty(inode, dirty);
  1531. }
  1532. ceph_put_cap_refs(ci, got);
  1533. unlock:
  1534. inode_unlock(inode);
  1535. ceph_free_cap_flush(prealloc_cf);
  1536. return ret;
  1537. }
  1538. const struct file_operations ceph_file_fops = {
  1539. .open = ceph_open,
  1540. .release = ceph_release,
  1541. .llseek = ceph_llseek,
  1542. .read_iter = ceph_read_iter,
  1543. .write_iter = ceph_write_iter,
  1544. .mmap = ceph_mmap,
  1545. .fsync = ceph_fsync,
  1546. .lock = ceph_lock,
  1547. .flock = ceph_flock,
  1548. .splice_read = generic_file_splice_read,
  1549. .splice_write = iter_file_splice_write,
  1550. .unlocked_ioctl = ceph_ioctl,
  1551. .compat_ioctl = ceph_ioctl,
  1552. .fallocate = ceph_fallocate,
  1553. };