file.c 45 KB

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