file.c 43 KB

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