file.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/ceph/striper.h>
  4. #include <linux/module.h>
  5. #include <linux/sched.h>
  6. #include <linux/slab.h>
  7. #include <linux/file.h>
  8. #include <linux/mount.h>
  9. #include <linux/namei.h>
  10. #include <linux/writeback.h>
  11. #include <linux/falloc.h>
  12. #include "super.h"
  13. #include "mds_client.h"
  14. #include "cache.h"
  15. static __le32 ceph_flags_sys2wire(u32 flags)
  16. {
  17. u32 wire_flags = 0;
  18. switch (flags & O_ACCMODE) {
  19. case O_RDONLY:
  20. wire_flags |= CEPH_O_RDONLY;
  21. break;
  22. case O_WRONLY:
  23. wire_flags |= CEPH_O_WRONLY;
  24. break;
  25. case O_RDWR:
  26. wire_flags |= CEPH_O_RDWR;
  27. break;
  28. }
  29. flags &= ~O_ACCMODE;
  30. #define ceph_sys2wire(a) if (flags & a) { wire_flags |= CEPH_##a; flags &= ~a; }
  31. ceph_sys2wire(O_CREAT);
  32. ceph_sys2wire(O_EXCL);
  33. ceph_sys2wire(O_TRUNC);
  34. ceph_sys2wire(O_DIRECTORY);
  35. ceph_sys2wire(O_NOFOLLOW);
  36. #undef ceph_sys2wire
  37. if (flags)
  38. dout("unused open flags: %x\n", flags);
  39. return cpu_to_le32(wire_flags);
  40. }
  41. /*
  42. * Ceph file operations
  43. *
  44. * Implement basic open/close functionality, and implement
  45. * read/write.
  46. *
  47. * We implement three modes of file I/O:
  48. * - buffered uses the generic_file_aio_{read,write} helpers
  49. *
  50. * - synchronous is used when there is multi-client read/write
  51. * sharing, avoids the page cache, and synchronously waits for an
  52. * ack from the OSD.
  53. *
  54. * - direct io takes the variant of the sync path that references
  55. * user pages directly.
  56. *
  57. * fsync() flushes and waits on dirty pages, but just queues metadata
  58. * for writeback: since the MDS can recover size and mtime there is no
  59. * need to wait for MDS acknowledgement.
  60. */
  61. /*
  62. * How many pages to get in one call to iov_iter_get_pages(). This
  63. * determines the size of the on-stack array used as a buffer.
  64. */
  65. #define ITER_GET_BVECS_PAGES 64
  66. static ssize_t __iter_get_bvecs(struct iov_iter *iter, size_t maxsize,
  67. struct bio_vec *bvecs)
  68. {
  69. size_t size = 0;
  70. int bvec_idx = 0;
  71. if (maxsize > iov_iter_count(iter))
  72. maxsize = iov_iter_count(iter);
  73. while (size < maxsize) {
  74. struct page *pages[ITER_GET_BVECS_PAGES];
  75. ssize_t bytes;
  76. size_t start;
  77. int idx = 0;
  78. bytes = iov_iter_get_pages(iter, pages, maxsize - size,
  79. ITER_GET_BVECS_PAGES, &start);
  80. if (bytes < 0)
  81. return size ?: bytes;
  82. iov_iter_advance(iter, bytes);
  83. size += bytes;
  84. for ( ; bytes; idx++, bvec_idx++) {
  85. struct bio_vec bv = {
  86. .bv_page = pages[idx],
  87. .bv_len = min_t(int, bytes, PAGE_SIZE - start),
  88. .bv_offset = start,
  89. };
  90. bvecs[bvec_idx] = bv;
  91. bytes -= bv.bv_len;
  92. start = 0;
  93. }
  94. }
  95. return size;
  96. }
  97. /*
  98. * iov_iter_get_pages() only considers one iov_iter segment, no matter
  99. * what maxsize or maxpages are given. For ITER_BVEC that is a single
  100. * page.
  101. *
  102. * Attempt to get up to @maxsize bytes worth of pages from @iter.
  103. * Return the number of bytes in the created bio_vec array, or an error.
  104. */
  105. static ssize_t iter_get_bvecs_alloc(struct iov_iter *iter, size_t maxsize,
  106. struct bio_vec **bvecs, int *num_bvecs)
  107. {
  108. struct bio_vec *bv;
  109. size_t orig_count = iov_iter_count(iter);
  110. ssize_t bytes;
  111. int npages;
  112. iov_iter_truncate(iter, maxsize);
  113. npages = iov_iter_npages(iter, INT_MAX);
  114. iov_iter_reexpand(iter, orig_count);
  115. /*
  116. * __iter_get_bvecs() may populate only part of the array -- zero it
  117. * out.
  118. */
  119. bv = kvmalloc_array(npages, sizeof(*bv), GFP_KERNEL | __GFP_ZERO);
  120. if (!bv)
  121. return -ENOMEM;
  122. bytes = __iter_get_bvecs(iter, maxsize, bv);
  123. if (bytes < 0) {
  124. /*
  125. * No pages were pinned -- just free the array.
  126. */
  127. kvfree(bv);
  128. return bytes;
  129. }
  130. *bvecs = bv;
  131. *num_bvecs = npages;
  132. return bytes;
  133. }
  134. static void put_bvecs(struct bio_vec *bvecs, int num_bvecs, bool should_dirty)
  135. {
  136. int i;
  137. for (i = 0; i < num_bvecs; i++) {
  138. if (bvecs[i].bv_page) {
  139. if (should_dirty)
  140. set_page_dirty_lock(bvecs[i].bv_page);
  141. put_page(bvecs[i].bv_page);
  142. }
  143. }
  144. kvfree(bvecs);
  145. }
  146. /*
  147. * Prepare an open request. Preallocate ceph_cap to avoid an
  148. * inopportune ENOMEM later.
  149. */
  150. static struct ceph_mds_request *
  151. prepare_open_request(struct super_block *sb, int flags, int create_mode)
  152. {
  153. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  154. struct ceph_mds_client *mdsc = fsc->mdsc;
  155. struct ceph_mds_request *req;
  156. int want_auth = USE_ANY_MDS;
  157. int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
  158. if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
  159. want_auth = USE_AUTH_MDS;
  160. req = ceph_mdsc_create_request(mdsc, op, want_auth);
  161. if (IS_ERR(req))
  162. goto out;
  163. req->r_fmode = ceph_flags_to_mode(flags);
  164. req->r_args.open.flags = ceph_flags_sys2wire(flags);
  165. req->r_args.open.mode = cpu_to_le32(create_mode);
  166. out:
  167. return req;
  168. }
  169. static int ceph_init_file_info(struct inode *inode, struct file *file,
  170. int fmode, bool isdir)
  171. {
  172. struct ceph_file_info *fi;
  173. dout("%s %p %p 0%o (%s)\n", __func__, inode, file,
  174. inode->i_mode, isdir ? "dir" : "regular");
  175. BUG_ON(inode->i_fop->release != ceph_release);
  176. if (isdir) {
  177. struct ceph_dir_file_info *dfi =
  178. kmem_cache_zalloc(ceph_dir_file_cachep, GFP_KERNEL);
  179. if (!dfi) {
  180. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  181. return -ENOMEM;
  182. }
  183. file->private_data = dfi;
  184. fi = &dfi->file_info;
  185. dfi->next_offset = 2;
  186. dfi->readdir_cache_idx = -1;
  187. } else {
  188. fi = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
  189. if (!fi) {
  190. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  191. return -ENOMEM;
  192. }
  193. file->private_data = fi;
  194. }
  195. fi->fmode = fmode;
  196. spin_lock_init(&fi->rw_contexts_lock);
  197. INIT_LIST_HEAD(&fi->rw_contexts);
  198. return 0;
  199. }
  200. /*
  201. * initialize private struct file data.
  202. * if we fail, clean up by dropping fmode reference on the ceph_inode
  203. */
  204. static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
  205. {
  206. int ret = 0;
  207. switch (inode->i_mode & S_IFMT) {
  208. case S_IFREG:
  209. ceph_fscache_register_inode_cookie(inode);
  210. ceph_fscache_file_set_cookie(inode, file);
  211. case S_IFDIR:
  212. ret = ceph_init_file_info(inode, file, fmode,
  213. S_ISDIR(inode->i_mode));
  214. if (ret)
  215. return ret;
  216. break;
  217. case S_IFLNK:
  218. dout("init_file %p %p 0%o (symlink)\n", inode, file,
  219. inode->i_mode);
  220. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  221. break;
  222. default:
  223. dout("init_file %p %p 0%o (special)\n", inode, file,
  224. inode->i_mode);
  225. /*
  226. * we need to drop the open ref now, since we don't
  227. * have .release set to ceph_release.
  228. */
  229. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  230. BUG_ON(inode->i_fop->release == ceph_release);
  231. /* call the proper open fop */
  232. ret = inode->i_fop->open(inode, file);
  233. }
  234. return ret;
  235. }
  236. /*
  237. * try renew caps after session gets killed.
  238. */
  239. int ceph_renew_caps(struct inode *inode)
  240. {
  241. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  242. struct ceph_inode_info *ci = ceph_inode(inode);
  243. struct ceph_mds_request *req;
  244. int err, flags, wanted;
  245. spin_lock(&ci->i_ceph_lock);
  246. wanted = __ceph_caps_file_wanted(ci);
  247. if (__ceph_is_any_real_caps(ci) &&
  248. (!(wanted & CEPH_CAP_ANY_WR) || ci->i_auth_cap)) {
  249. int issued = __ceph_caps_issued(ci, NULL);
  250. spin_unlock(&ci->i_ceph_lock);
  251. dout("renew caps %p want %s issued %s updating mds_wanted\n",
  252. inode, ceph_cap_string(wanted), ceph_cap_string(issued));
  253. ceph_check_caps(ci, 0, NULL);
  254. return 0;
  255. }
  256. spin_unlock(&ci->i_ceph_lock);
  257. flags = 0;
  258. if ((wanted & CEPH_CAP_FILE_RD) && (wanted & CEPH_CAP_FILE_WR))
  259. flags = O_RDWR;
  260. else if (wanted & CEPH_CAP_FILE_RD)
  261. flags = O_RDONLY;
  262. else if (wanted & CEPH_CAP_FILE_WR)
  263. flags = O_WRONLY;
  264. #ifdef O_LAZY
  265. if (wanted & CEPH_CAP_FILE_LAZYIO)
  266. flags |= O_LAZY;
  267. #endif
  268. req = prepare_open_request(inode->i_sb, flags, 0);
  269. if (IS_ERR(req)) {
  270. err = PTR_ERR(req);
  271. goto out;
  272. }
  273. req->r_inode = inode;
  274. ihold(inode);
  275. req->r_num_caps = 1;
  276. req->r_fmode = -1;
  277. err = ceph_mdsc_do_request(mdsc, NULL, req);
  278. ceph_mdsc_put_request(req);
  279. out:
  280. dout("renew caps %p open result=%d\n", inode, err);
  281. return err < 0 ? err : 0;
  282. }
  283. /*
  284. * If we already have the requisite capabilities, we can satisfy
  285. * the open request locally (no need to request new caps from the
  286. * MDS). We do, however, need to inform the MDS (asynchronously)
  287. * if our wanted caps set expands.
  288. */
  289. int ceph_open(struct inode *inode, struct file *file)
  290. {
  291. struct ceph_inode_info *ci = ceph_inode(inode);
  292. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  293. struct ceph_mds_client *mdsc = fsc->mdsc;
  294. struct ceph_mds_request *req;
  295. struct ceph_file_info *fi = file->private_data;
  296. int err;
  297. int flags, fmode, wanted;
  298. if (fi) {
  299. dout("open file %p is already opened\n", file);
  300. return 0;
  301. }
  302. /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
  303. flags = file->f_flags & ~(O_CREAT|O_EXCL);
  304. if (S_ISDIR(inode->i_mode))
  305. flags = O_DIRECTORY; /* mds likes to know */
  306. dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
  307. ceph_vinop(inode), file, flags, file->f_flags);
  308. fmode = ceph_flags_to_mode(flags);
  309. wanted = ceph_caps_for_mode(fmode);
  310. /* snapped files are read-only */
  311. if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
  312. return -EROFS;
  313. /* trivially open snapdir */
  314. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  315. spin_lock(&ci->i_ceph_lock);
  316. __ceph_get_fmode(ci, fmode);
  317. spin_unlock(&ci->i_ceph_lock);
  318. return ceph_init_file(inode, file, fmode);
  319. }
  320. /*
  321. * No need to block if we have caps on the auth MDS (for
  322. * write) or any MDS (for read). Update wanted set
  323. * asynchronously.
  324. */
  325. spin_lock(&ci->i_ceph_lock);
  326. if (__ceph_is_any_real_caps(ci) &&
  327. (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
  328. int mds_wanted = __ceph_caps_mds_wanted(ci, true);
  329. int issued = __ceph_caps_issued(ci, NULL);
  330. dout("open %p fmode %d want %s issued %s using existing\n",
  331. inode, fmode, ceph_cap_string(wanted),
  332. ceph_cap_string(issued));
  333. __ceph_get_fmode(ci, fmode);
  334. spin_unlock(&ci->i_ceph_lock);
  335. /* adjust wanted? */
  336. if ((issued & wanted) != wanted &&
  337. (mds_wanted & wanted) != wanted &&
  338. ceph_snap(inode) != CEPH_SNAPDIR)
  339. ceph_check_caps(ci, 0, NULL);
  340. return ceph_init_file(inode, file, fmode);
  341. } else if (ceph_snap(inode) != CEPH_NOSNAP &&
  342. (ci->i_snap_caps & wanted) == wanted) {
  343. __ceph_get_fmode(ci, fmode);
  344. spin_unlock(&ci->i_ceph_lock);
  345. return ceph_init_file(inode, file, fmode);
  346. }
  347. spin_unlock(&ci->i_ceph_lock);
  348. dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
  349. req = prepare_open_request(inode->i_sb, flags, 0);
  350. if (IS_ERR(req)) {
  351. err = PTR_ERR(req);
  352. goto out;
  353. }
  354. req->r_inode = inode;
  355. ihold(inode);
  356. req->r_num_caps = 1;
  357. err = ceph_mdsc_do_request(mdsc, NULL, req);
  358. if (!err)
  359. err = ceph_init_file(inode, file, req->r_fmode);
  360. ceph_mdsc_put_request(req);
  361. dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
  362. out:
  363. return err;
  364. }
  365. /*
  366. * Do a lookup + open with a single request. If we get a non-existent
  367. * file or symlink, return 1 so the VFS can retry.
  368. */
  369. int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
  370. struct file *file, unsigned flags, umode_t mode)
  371. {
  372. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  373. struct ceph_mds_client *mdsc = fsc->mdsc;
  374. struct ceph_mds_request *req;
  375. struct dentry *dn;
  376. struct ceph_acls_info acls = {};
  377. int mask;
  378. int err;
  379. dout("atomic_open %p dentry %p '%pd' %s flags %d mode 0%o\n",
  380. dir, dentry, dentry,
  381. d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
  382. if (dentry->d_name.len > NAME_MAX)
  383. return -ENAMETOOLONG;
  384. if (flags & O_CREAT) {
  385. if (ceph_quota_is_max_files_exceeded(dir))
  386. return -EDQUOT;
  387. err = ceph_pre_init_acls(dir, &mode, &acls);
  388. if (err < 0)
  389. return err;
  390. }
  391. /* do the open */
  392. req = prepare_open_request(dir->i_sb, flags, mode);
  393. if (IS_ERR(req)) {
  394. err = PTR_ERR(req);
  395. goto out_acl;
  396. }
  397. req->r_dentry = dget(dentry);
  398. req->r_num_caps = 2;
  399. if (flags & O_CREAT) {
  400. req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
  401. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  402. if (acls.pagelist) {
  403. req->r_pagelist = acls.pagelist;
  404. acls.pagelist = NULL;
  405. }
  406. }
  407. mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
  408. if (ceph_security_xattr_wanted(dir))
  409. mask |= CEPH_CAP_XATTR_SHARED;
  410. req->r_args.open.mask = cpu_to_le32(mask);
  411. req->r_parent = dir;
  412. set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
  413. err = ceph_mdsc_do_request(mdsc,
  414. (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
  415. req);
  416. err = ceph_handle_snapdir(req, dentry, err);
  417. if (err)
  418. goto out_req;
  419. if ((flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  420. err = ceph_handle_notrace_create(dir, dentry);
  421. if (d_in_lookup(dentry)) {
  422. dn = ceph_finish_lookup(req, dentry, err);
  423. if (IS_ERR(dn))
  424. err = PTR_ERR(dn);
  425. } else {
  426. /* we were given a hashed negative dentry */
  427. dn = NULL;
  428. }
  429. if (err)
  430. goto out_req;
  431. if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
  432. /* make vfs retry on splice, ENOENT, or symlink */
  433. dout("atomic_open finish_no_open on dn %p\n", dn);
  434. err = finish_no_open(file, dn);
  435. } else {
  436. dout("atomic_open finish_open on dn %p\n", dn);
  437. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  438. ceph_init_inode_acls(d_inode(dentry), &acls);
  439. file->f_mode |= FMODE_CREATED;
  440. }
  441. err = finish_open(file, dentry, ceph_open);
  442. }
  443. out_req:
  444. if (!req->r_err && req->r_target_inode)
  445. ceph_put_fmode(ceph_inode(req->r_target_inode), req->r_fmode);
  446. ceph_mdsc_put_request(req);
  447. out_acl:
  448. ceph_release_acls_info(&acls);
  449. dout("atomic_open result=%d\n", err);
  450. return err;
  451. }
  452. int ceph_release(struct inode *inode, struct file *file)
  453. {
  454. struct ceph_inode_info *ci = ceph_inode(inode);
  455. if (S_ISDIR(inode->i_mode)) {
  456. struct ceph_dir_file_info *dfi = file->private_data;
  457. dout("release inode %p dir file %p\n", inode, file);
  458. WARN_ON(!list_empty(&dfi->file_info.rw_contexts));
  459. ceph_put_fmode(ci, dfi->file_info.fmode);
  460. if (dfi->last_readdir)
  461. ceph_mdsc_put_request(dfi->last_readdir);
  462. kfree(dfi->last_name);
  463. kfree(dfi->dir_info);
  464. kmem_cache_free(ceph_dir_file_cachep, dfi);
  465. } else {
  466. struct ceph_file_info *fi = file->private_data;
  467. dout("release inode %p regular file %p\n", inode, file);
  468. WARN_ON(!list_empty(&fi->rw_contexts));
  469. ceph_put_fmode(ci, fi->fmode);
  470. kmem_cache_free(ceph_file_cachep, fi);
  471. }
  472. /* wake up anyone waiting for caps on this inode */
  473. wake_up_all(&ci->i_cap_wq);
  474. return 0;
  475. }
  476. enum {
  477. HAVE_RETRIED = 1,
  478. CHECK_EOF = 2,
  479. READ_INLINE = 3,
  480. };
  481. /*
  482. * Completely synchronous read and write methods. Direct from __user
  483. * buffer to osd, or directly to user pages (if O_DIRECT).
  484. *
  485. * If the read spans object boundary, just do multiple reads. (That's not
  486. * atomic, but good enough for now.)
  487. *
  488. * If we get a short result from the OSD, check against i_size; we need to
  489. * only return a short read to the caller if we hit EOF.
  490. */
  491. static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,
  492. int *retry_op)
  493. {
  494. struct file *file = iocb->ki_filp;
  495. struct inode *inode = file_inode(file);
  496. struct ceph_inode_info *ci = ceph_inode(inode);
  497. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  498. struct ceph_osd_client *osdc = &fsc->client->osdc;
  499. ssize_t ret;
  500. u64 off = iocb->ki_pos;
  501. u64 len = iov_iter_count(to);
  502. dout("sync_read on file %p %llu~%u %s\n", file, off, (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, off + len);
  513. if (ret < 0)
  514. return ret;
  515. ret = 0;
  516. while ((len = iov_iter_count(to)) > 0) {
  517. struct ceph_osd_request *req;
  518. struct page **pages;
  519. int num_pages;
  520. size_t page_off;
  521. u64 i_size;
  522. bool more;
  523. req = ceph_osdc_new_request(osdc, &ci->i_layout,
  524. ci->i_vino, off, &len, 0, 1,
  525. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  526. NULL, ci->i_truncate_seq,
  527. ci->i_truncate_size, false);
  528. if (IS_ERR(req)) {
  529. ret = PTR_ERR(req);
  530. break;
  531. }
  532. more = len < iov_iter_count(to);
  533. if (unlikely(iov_iter_is_pipe(to))) {
  534. ret = iov_iter_get_pages_alloc(to, &pages, len,
  535. &page_off);
  536. if (ret <= 0) {
  537. ceph_osdc_put_request(req);
  538. ret = -ENOMEM;
  539. break;
  540. }
  541. num_pages = DIV_ROUND_UP(ret + page_off, PAGE_SIZE);
  542. if (ret < len) {
  543. len = ret;
  544. osd_req_op_extent_update(req, 0, len);
  545. more = false;
  546. }
  547. } else {
  548. num_pages = calc_pages_for(off, len);
  549. page_off = off & ~PAGE_MASK;
  550. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  551. if (IS_ERR(pages)) {
  552. ceph_osdc_put_request(req);
  553. ret = PTR_ERR(pages);
  554. break;
  555. }
  556. }
  557. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_off,
  558. false, false);
  559. ret = ceph_osdc_start_request(osdc, req, false);
  560. if (!ret)
  561. ret = ceph_osdc_wait_request(osdc, req);
  562. ceph_osdc_put_request(req);
  563. i_size = i_size_read(inode);
  564. dout("sync_read %llu~%llu got %zd i_size %llu%s\n",
  565. off, len, ret, i_size, (more ? " MORE" : ""));
  566. if (ret == -ENOENT)
  567. ret = 0;
  568. if (ret >= 0 && ret < len && (off + ret < i_size)) {
  569. int zlen = min(len - ret, i_size - off - ret);
  570. int zoff = page_off + ret;
  571. dout("sync_read zero gap %llu~%llu\n",
  572. off + ret, off + ret + zlen);
  573. ceph_zero_page_vector_range(zoff, zlen, pages);
  574. ret += zlen;
  575. }
  576. if (unlikely(iov_iter_is_pipe(to))) {
  577. if (ret > 0) {
  578. iov_iter_advance(to, ret);
  579. off += ret;
  580. } else {
  581. iov_iter_advance(to, 0);
  582. }
  583. ceph_put_page_vector(pages, num_pages, false);
  584. } else {
  585. int idx = 0;
  586. size_t left = ret > 0 ? ret : 0;
  587. while (left > 0) {
  588. size_t len, copied;
  589. page_off = off & ~PAGE_MASK;
  590. len = min_t(size_t, left, PAGE_SIZE - page_off);
  591. copied = copy_page_to_iter(pages[idx++],
  592. page_off, len, to);
  593. off += copied;
  594. left -= copied;
  595. if (copied < len) {
  596. ret = -EFAULT;
  597. break;
  598. }
  599. }
  600. ceph_release_page_vector(pages, num_pages);
  601. }
  602. if (ret <= 0 || off >= i_size || !more)
  603. break;
  604. }
  605. if (off > iocb->ki_pos) {
  606. if (ret >= 0 &&
  607. iov_iter_count(to) > 0 && off >= i_size_read(inode))
  608. *retry_op = CHECK_EOF;
  609. ret = off - iocb->ki_pos;
  610. iocb->ki_pos = off;
  611. }
  612. dout("sync_read result %zd retry_op %d\n", ret, *retry_op);
  613. return ret;
  614. }
  615. struct ceph_aio_request {
  616. struct kiocb *iocb;
  617. size_t total_len;
  618. bool write;
  619. bool should_dirty;
  620. int error;
  621. struct list_head osd_reqs;
  622. unsigned num_reqs;
  623. atomic_t pending_reqs;
  624. struct timespec64 mtime;
  625. struct ceph_cap_flush *prealloc_cf;
  626. };
  627. struct ceph_aio_work {
  628. struct work_struct work;
  629. struct ceph_osd_request *req;
  630. };
  631. static void ceph_aio_retry_work(struct work_struct *work);
  632. static void ceph_aio_complete(struct inode *inode,
  633. struct ceph_aio_request *aio_req)
  634. {
  635. struct ceph_inode_info *ci = ceph_inode(inode);
  636. int ret;
  637. if (!atomic_dec_and_test(&aio_req->pending_reqs))
  638. return;
  639. ret = aio_req->error;
  640. if (!ret)
  641. ret = aio_req->total_len;
  642. dout("ceph_aio_complete %p rc %d\n", inode, ret);
  643. if (ret >= 0 && aio_req->write) {
  644. int dirty;
  645. loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
  646. if (endoff > i_size_read(inode)) {
  647. if (ceph_inode_set_size(inode, endoff))
  648. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  649. }
  650. spin_lock(&ci->i_ceph_lock);
  651. ci->i_inline_version = CEPH_INLINE_NONE;
  652. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  653. &aio_req->prealloc_cf);
  654. spin_unlock(&ci->i_ceph_lock);
  655. if (dirty)
  656. __mark_inode_dirty(inode, dirty);
  657. }
  658. ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
  659. CEPH_CAP_FILE_RD));
  660. aio_req->iocb->ki_complete(aio_req->iocb, ret, 0);
  661. ceph_free_cap_flush(aio_req->prealloc_cf);
  662. kfree(aio_req);
  663. }
  664. static void ceph_aio_complete_req(struct ceph_osd_request *req)
  665. {
  666. int rc = req->r_result;
  667. struct inode *inode = req->r_inode;
  668. struct ceph_aio_request *aio_req = req->r_priv;
  669. struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
  670. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_BVECS);
  671. BUG_ON(!osd_data->num_bvecs);
  672. dout("ceph_aio_complete_req %p rc %d bytes %u\n",
  673. inode, rc, osd_data->bvec_pos.iter.bi_size);
  674. if (rc == -EOLDSNAPC) {
  675. struct ceph_aio_work *aio_work;
  676. BUG_ON(!aio_req->write);
  677. aio_work = kmalloc(sizeof(*aio_work), GFP_NOFS);
  678. if (aio_work) {
  679. INIT_WORK(&aio_work->work, ceph_aio_retry_work);
  680. aio_work->req = req;
  681. queue_work(ceph_inode_to_client(inode)->wb_wq,
  682. &aio_work->work);
  683. return;
  684. }
  685. rc = -ENOMEM;
  686. } else if (!aio_req->write) {
  687. if (rc == -ENOENT)
  688. rc = 0;
  689. if (rc >= 0 && osd_data->bvec_pos.iter.bi_size > rc) {
  690. struct iov_iter i;
  691. int zlen = osd_data->bvec_pos.iter.bi_size - rc;
  692. /*
  693. * If read is satisfied by single OSD request,
  694. * it can pass EOF. Otherwise read is within
  695. * i_size.
  696. */
  697. if (aio_req->num_reqs == 1) {
  698. loff_t i_size = i_size_read(inode);
  699. loff_t endoff = aio_req->iocb->ki_pos + rc;
  700. if (endoff < i_size)
  701. zlen = min_t(size_t, zlen,
  702. i_size - endoff);
  703. aio_req->total_len = rc + zlen;
  704. }
  705. iov_iter_bvec(&i, READ, osd_data->bvec_pos.bvecs,
  706. osd_data->num_bvecs,
  707. osd_data->bvec_pos.iter.bi_size);
  708. iov_iter_advance(&i, rc);
  709. iov_iter_zero(zlen, &i);
  710. }
  711. }
  712. put_bvecs(osd_data->bvec_pos.bvecs, osd_data->num_bvecs,
  713. aio_req->should_dirty);
  714. ceph_osdc_put_request(req);
  715. if (rc < 0)
  716. cmpxchg(&aio_req->error, 0, rc);
  717. ceph_aio_complete(inode, aio_req);
  718. return;
  719. }
  720. static void ceph_aio_retry_work(struct work_struct *work)
  721. {
  722. struct ceph_aio_work *aio_work =
  723. container_of(work, struct ceph_aio_work, work);
  724. struct ceph_osd_request *orig_req = aio_work->req;
  725. struct ceph_aio_request *aio_req = orig_req->r_priv;
  726. struct inode *inode = orig_req->r_inode;
  727. struct ceph_inode_info *ci = ceph_inode(inode);
  728. struct ceph_snap_context *snapc;
  729. struct ceph_osd_request *req;
  730. int ret;
  731. spin_lock(&ci->i_ceph_lock);
  732. if (__ceph_have_pending_cap_snap(ci)) {
  733. struct ceph_cap_snap *capsnap =
  734. list_last_entry(&ci->i_cap_snaps,
  735. struct ceph_cap_snap,
  736. ci_item);
  737. snapc = ceph_get_snap_context(capsnap->context);
  738. } else {
  739. BUG_ON(!ci->i_head_snapc);
  740. snapc = ceph_get_snap_context(ci->i_head_snapc);
  741. }
  742. spin_unlock(&ci->i_ceph_lock);
  743. req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 1,
  744. false, GFP_NOFS);
  745. if (!req) {
  746. ret = -ENOMEM;
  747. req = orig_req;
  748. goto out;
  749. }
  750. req->r_flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
  751. ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
  752. ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
  753. req->r_ops[0] = orig_req->r_ops[0];
  754. req->r_mtime = aio_req->mtime;
  755. req->r_data_offset = req->r_ops[0].extent.offset;
  756. ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
  757. if (ret) {
  758. ceph_osdc_put_request(req);
  759. req = orig_req;
  760. goto out;
  761. }
  762. ceph_osdc_put_request(orig_req);
  763. req->r_callback = ceph_aio_complete_req;
  764. req->r_inode = inode;
  765. req->r_priv = aio_req;
  766. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  767. out:
  768. if (ret < 0) {
  769. req->r_result = ret;
  770. ceph_aio_complete_req(req);
  771. }
  772. ceph_put_snap_context(snapc);
  773. kfree(aio_work);
  774. }
  775. static ssize_t
  776. ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
  777. struct ceph_snap_context *snapc,
  778. struct ceph_cap_flush **pcf)
  779. {
  780. struct file *file = iocb->ki_filp;
  781. struct inode *inode = file_inode(file);
  782. struct ceph_inode_info *ci = ceph_inode(inode);
  783. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  784. struct ceph_vino vino;
  785. struct ceph_osd_request *req;
  786. struct bio_vec *bvecs;
  787. struct ceph_aio_request *aio_req = NULL;
  788. int num_pages = 0;
  789. int flags;
  790. int ret;
  791. struct timespec64 mtime = current_time(inode);
  792. size_t count = iov_iter_count(iter);
  793. loff_t pos = iocb->ki_pos;
  794. bool write = iov_iter_rw(iter) == WRITE;
  795. bool should_dirty = !write && iter_is_iovec(iter);
  796. if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  797. return -EROFS;
  798. dout("sync_direct_%s on file %p %lld~%u snapc %p seq %lld\n",
  799. (write ? "write" : "read"), file, pos, (unsigned)count,
  800. snapc, snapc->seq);
  801. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  802. if (ret < 0)
  803. return ret;
  804. if (write) {
  805. int ret2 = invalidate_inode_pages2_range(inode->i_mapping,
  806. pos >> PAGE_SHIFT,
  807. (pos + count) >> PAGE_SHIFT);
  808. if (ret2 < 0)
  809. dout("invalidate_inode_pages2_range returned %d\n", ret2);
  810. flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
  811. } else {
  812. flags = CEPH_OSD_FLAG_READ;
  813. }
  814. while (iov_iter_count(iter) > 0) {
  815. u64 size = iov_iter_count(iter);
  816. ssize_t len;
  817. if (write)
  818. size = min_t(u64, size, fsc->mount_options->wsize);
  819. else
  820. size = min_t(u64, size, fsc->mount_options->rsize);
  821. vino = ceph_vino(inode);
  822. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  823. vino, pos, &size, 0,
  824. 1,
  825. write ? CEPH_OSD_OP_WRITE :
  826. CEPH_OSD_OP_READ,
  827. flags, snapc,
  828. ci->i_truncate_seq,
  829. ci->i_truncate_size,
  830. false);
  831. if (IS_ERR(req)) {
  832. ret = PTR_ERR(req);
  833. break;
  834. }
  835. len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages);
  836. if (len < 0) {
  837. ceph_osdc_put_request(req);
  838. ret = len;
  839. break;
  840. }
  841. if (len != size)
  842. osd_req_op_extent_update(req, 0, len);
  843. /*
  844. * To simplify error handling, allow AIO when IO within i_size
  845. * or IO can be satisfied by single OSD request.
  846. */
  847. if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
  848. (len == count || pos + count <= i_size_read(inode))) {
  849. aio_req = kzalloc(sizeof(*aio_req), GFP_KERNEL);
  850. if (aio_req) {
  851. aio_req->iocb = iocb;
  852. aio_req->write = write;
  853. aio_req->should_dirty = should_dirty;
  854. INIT_LIST_HEAD(&aio_req->osd_reqs);
  855. if (write) {
  856. aio_req->mtime = mtime;
  857. swap(aio_req->prealloc_cf, *pcf);
  858. }
  859. }
  860. /* ignore error */
  861. }
  862. if (write) {
  863. /*
  864. * throw out any page cache pages in this range. this
  865. * may block.
  866. */
  867. truncate_inode_pages_range(inode->i_mapping, pos,
  868. (pos+len) | (PAGE_SIZE - 1));
  869. req->r_mtime = mtime;
  870. }
  871. osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len);
  872. if (aio_req) {
  873. aio_req->total_len += len;
  874. aio_req->num_reqs++;
  875. atomic_inc(&aio_req->pending_reqs);
  876. req->r_callback = ceph_aio_complete_req;
  877. req->r_inode = inode;
  878. req->r_priv = aio_req;
  879. list_add_tail(&req->r_unsafe_item, &aio_req->osd_reqs);
  880. pos += len;
  881. continue;
  882. }
  883. ret = ceph_osdc_start_request(req->r_osdc, req, false);
  884. if (!ret)
  885. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  886. size = i_size_read(inode);
  887. if (!write) {
  888. if (ret == -ENOENT)
  889. ret = 0;
  890. if (ret >= 0 && ret < len && pos + ret < size) {
  891. struct iov_iter i;
  892. int zlen = min_t(size_t, len - ret,
  893. size - pos - ret);
  894. iov_iter_bvec(&i, READ, bvecs, num_pages, len);
  895. iov_iter_advance(&i, ret);
  896. iov_iter_zero(zlen, &i);
  897. ret += zlen;
  898. }
  899. if (ret >= 0)
  900. len = ret;
  901. }
  902. put_bvecs(bvecs, num_pages, should_dirty);
  903. ceph_osdc_put_request(req);
  904. if (ret < 0)
  905. break;
  906. pos += len;
  907. if (!write && pos >= size)
  908. break;
  909. if (write && pos > size) {
  910. if (ceph_inode_set_size(inode, pos))
  911. ceph_check_caps(ceph_inode(inode),
  912. CHECK_CAPS_AUTHONLY,
  913. NULL);
  914. }
  915. }
  916. if (aio_req) {
  917. LIST_HEAD(osd_reqs);
  918. if (aio_req->num_reqs == 0) {
  919. kfree(aio_req);
  920. return ret;
  921. }
  922. ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
  923. CEPH_CAP_FILE_RD);
  924. list_splice(&aio_req->osd_reqs, &osd_reqs);
  925. while (!list_empty(&osd_reqs)) {
  926. req = list_first_entry(&osd_reqs,
  927. struct ceph_osd_request,
  928. r_unsafe_item);
  929. list_del_init(&req->r_unsafe_item);
  930. if (ret >= 0)
  931. ret = ceph_osdc_start_request(req->r_osdc,
  932. req, false);
  933. if (ret < 0) {
  934. req->r_result = ret;
  935. ceph_aio_complete_req(req);
  936. }
  937. }
  938. return -EIOCBQUEUED;
  939. }
  940. if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
  941. ret = pos - iocb->ki_pos;
  942. iocb->ki_pos = pos;
  943. }
  944. return ret;
  945. }
  946. /*
  947. * Synchronous write, straight from __user pointer or user pages.
  948. *
  949. * If write spans object boundary, just do multiple writes. (For a
  950. * correct atomic write, we should e.g. take write locks on all
  951. * objects, rollback on failure, etc.)
  952. */
  953. static ssize_t
  954. ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
  955. struct ceph_snap_context *snapc)
  956. {
  957. struct file *file = iocb->ki_filp;
  958. struct inode *inode = file_inode(file);
  959. struct ceph_inode_info *ci = ceph_inode(inode);
  960. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  961. struct ceph_vino vino;
  962. struct ceph_osd_request *req;
  963. struct page **pages;
  964. u64 len;
  965. int num_pages;
  966. int written = 0;
  967. int flags;
  968. int ret;
  969. bool check_caps = false;
  970. struct timespec64 mtime = current_time(inode);
  971. size_t count = iov_iter_count(from);
  972. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  973. return -EROFS;
  974. dout("sync_write on file %p %lld~%u snapc %p seq %lld\n",
  975. file, pos, (unsigned)count, snapc, snapc->seq);
  976. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  977. if (ret < 0)
  978. return ret;
  979. ret = invalidate_inode_pages2_range(inode->i_mapping,
  980. pos >> PAGE_SHIFT,
  981. (pos + count) >> PAGE_SHIFT);
  982. if (ret < 0)
  983. dout("invalidate_inode_pages2_range returned %d\n", ret);
  984. flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
  985. while ((len = iov_iter_count(from)) > 0) {
  986. size_t left;
  987. int n;
  988. vino = ceph_vino(inode);
  989. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  990. vino, pos, &len, 0, 1,
  991. CEPH_OSD_OP_WRITE, flags, snapc,
  992. ci->i_truncate_seq,
  993. ci->i_truncate_size,
  994. false);
  995. if (IS_ERR(req)) {
  996. ret = PTR_ERR(req);
  997. break;
  998. }
  999. /*
  1000. * write from beginning of first page,
  1001. * regardless of io alignment
  1002. */
  1003. num_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1004. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  1005. if (IS_ERR(pages)) {
  1006. ret = PTR_ERR(pages);
  1007. goto out;
  1008. }
  1009. left = len;
  1010. for (n = 0; n < num_pages; n++) {
  1011. size_t plen = min_t(size_t, left, PAGE_SIZE);
  1012. ret = copy_page_from_iter(pages[n], 0, plen, from);
  1013. if (ret != plen) {
  1014. ret = -EFAULT;
  1015. break;
  1016. }
  1017. left -= ret;
  1018. }
  1019. if (ret < 0) {
  1020. ceph_release_page_vector(pages, num_pages);
  1021. goto out;
  1022. }
  1023. req->r_inode = inode;
  1024. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
  1025. false, true);
  1026. req->r_mtime = mtime;
  1027. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1028. if (!ret)
  1029. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1030. out:
  1031. ceph_osdc_put_request(req);
  1032. if (ret != 0) {
  1033. ceph_set_error_write(ci);
  1034. break;
  1035. }
  1036. ceph_clear_error_write(ci);
  1037. pos += len;
  1038. written += len;
  1039. if (pos > i_size_read(inode)) {
  1040. check_caps = ceph_inode_set_size(inode, pos);
  1041. if (check_caps)
  1042. ceph_check_caps(ceph_inode(inode),
  1043. CHECK_CAPS_AUTHONLY,
  1044. NULL);
  1045. }
  1046. }
  1047. if (ret != -EOLDSNAPC && written > 0) {
  1048. ret = written;
  1049. iocb->ki_pos = pos;
  1050. }
  1051. return ret;
  1052. }
  1053. /*
  1054. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  1055. * Atomically grab references, so that those bits are not released
  1056. * back to the MDS mid-read.
  1057. *
  1058. * Hmm, the sync read case isn't actually async... should it be?
  1059. */
  1060. static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1061. {
  1062. struct file *filp = iocb->ki_filp;
  1063. struct ceph_file_info *fi = filp->private_data;
  1064. size_t len = iov_iter_count(to);
  1065. struct inode *inode = file_inode(filp);
  1066. struct ceph_inode_info *ci = ceph_inode(inode);
  1067. struct page *pinned_page = NULL;
  1068. ssize_t ret;
  1069. int want, got = 0;
  1070. int retry_op = 0, read = 0;
  1071. again:
  1072. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  1073. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
  1074. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1075. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  1076. else
  1077. want = CEPH_CAP_FILE_CACHE;
  1078. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
  1079. if (ret < 0)
  1080. return ret;
  1081. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  1082. (iocb->ki_flags & IOCB_DIRECT) ||
  1083. (fi->flags & CEPH_F_SYNC)) {
  1084. dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  1085. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1086. ceph_cap_string(got));
  1087. if (ci->i_inline_version == CEPH_INLINE_NONE) {
  1088. if (!retry_op && (iocb->ki_flags & IOCB_DIRECT)) {
  1089. ret = ceph_direct_read_write(iocb, to,
  1090. NULL, NULL);
  1091. if (ret >= 0 && ret < len)
  1092. retry_op = CHECK_EOF;
  1093. } else {
  1094. ret = ceph_sync_read(iocb, to, &retry_op);
  1095. }
  1096. } else {
  1097. retry_op = READ_INLINE;
  1098. }
  1099. } else {
  1100. CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
  1101. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  1102. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1103. ceph_cap_string(got));
  1104. ceph_add_rw_context(fi, &rw_ctx);
  1105. ret = generic_file_read_iter(iocb, to);
  1106. ceph_del_rw_context(fi, &rw_ctx);
  1107. }
  1108. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  1109. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  1110. if (pinned_page) {
  1111. put_page(pinned_page);
  1112. pinned_page = NULL;
  1113. }
  1114. ceph_put_cap_refs(ci, got);
  1115. if (retry_op > HAVE_RETRIED && ret >= 0) {
  1116. int statret;
  1117. struct page *page = NULL;
  1118. loff_t i_size;
  1119. if (retry_op == READ_INLINE) {
  1120. page = __page_cache_alloc(GFP_KERNEL);
  1121. if (!page)
  1122. return -ENOMEM;
  1123. }
  1124. statret = __ceph_do_getattr(inode, page,
  1125. CEPH_STAT_CAP_INLINE_DATA, !!page);
  1126. if (statret < 0) {
  1127. if (page)
  1128. __free_page(page);
  1129. if (statret == -ENODATA) {
  1130. BUG_ON(retry_op != READ_INLINE);
  1131. goto again;
  1132. }
  1133. return statret;
  1134. }
  1135. i_size = i_size_read(inode);
  1136. if (retry_op == READ_INLINE) {
  1137. BUG_ON(ret > 0 || read > 0);
  1138. if (iocb->ki_pos < i_size &&
  1139. iocb->ki_pos < PAGE_SIZE) {
  1140. loff_t end = min_t(loff_t, i_size,
  1141. iocb->ki_pos + len);
  1142. end = min_t(loff_t, end, PAGE_SIZE);
  1143. if (statret < end)
  1144. zero_user_segment(page, statret, end);
  1145. ret = copy_page_to_iter(page,
  1146. iocb->ki_pos & ~PAGE_MASK,
  1147. end - iocb->ki_pos, to);
  1148. iocb->ki_pos += ret;
  1149. read += ret;
  1150. }
  1151. if (iocb->ki_pos < i_size && read < len) {
  1152. size_t zlen = min_t(size_t, len - read,
  1153. i_size - iocb->ki_pos);
  1154. ret = iov_iter_zero(zlen, to);
  1155. iocb->ki_pos += ret;
  1156. read += ret;
  1157. }
  1158. __free_pages(page, 0);
  1159. return read;
  1160. }
  1161. /* hit EOF or hole? */
  1162. if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
  1163. ret < len) {
  1164. dout("sync_read hit hole, ppos %lld < size %lld"
  1165. ", reading more\n", iocb->ki_pos, i_size);
  1166. read += ret;
  1167. len -= ret;
  1168. retry_op = HAVE_RETRIED;
  1169. goto again;
  1170. }
  1171. }
  1172. if (ret >= 0)
  1173. ret += read;
  1174. return ret;
  1175. }
  1176. /*
  1177. * Take cap references to avoid releasing caps to MDS mid-write.
  1178. *
  1179. * If we are synchronous, and write with an old snap context, the OSD
  1180. * may return EOLDSNAPC. In that case, retry the write.. _after_
  1181. * dropping our cap refs and allowing the pending snap to logically
  1182. * complete _before_ this write occurs.
  1183. *
  1184. * If we are near ENOSPC, write synchronously.
  1185. */
  1186. static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1187. {
  1188. struct file *file = iocb->ki_filp;
  1189. struct ceph_file_info *fi = file->private_data;
  1190. struct inode *inode = file_inode(file);
  1191. struct ceph_inode_info *ci = ceph_inode(inode);
  1192. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1193. struct ceph_cap_flush *prealloc_cf;
  1194. ssize_t count, written = 0;
  1195. int err, want, got;
  1196. loff_t pos;
  1197. loff_t limit = max(i_size_read(inode), fsc->max_file_size);
  1198. if (ceph_snap(inode) != CEPH_NOSNAP)
  1199. return -EROFS;
  1200. prealloc_cf = ceph_alloc_cap_flush();
  1201. if (!prealloc_cf)
  1202. return -ENOMEM;
  1203. retry_snap:
  1204. inode_lock(inode);
  1205. /* We can write back this queue in page reclaim */
  1206. current->backing_dev_info = inode_to_bdi(inode);
  1207. if (iocb->ki_flags & IOCB_APPEND) {
  1208. err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1209. if (err < 0)
  1210. goto out;
  1211. }
  1212. err = generic_write_checks(iocb, from);
  1213. if (err <= 0)
  1214. goto out;
  1215. pos = iocb->ki_pos;
  1216. if (unlikely(pos >= limit)) {
  1217. err = -EFBIG;
  1218. goto out;
  1219. } else {
  1220. iov_iter_truncate(from, limit - pos);
  1221. }
  1222. count = iov_iter_count(from);
  1223. if (ceph_quota_is_max_bytes_exceeded(inode, pos + count)) {
  1224. err = -EDQUOT;
  1225. goto out;
  1226. }
  1227. err = file_remove_privs(file);
  1228. if (err)
  1229. goto out;
  1230. err = file_update_time(file);
  1231. if (err)
  1232. goto out;
  1233. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1234. err = ceph_uninline_data(file, NULL);
  1235. if (err < 0)
  1236. goto out;
  1237. }
  1238. /* FIXME: not complete since it doesn't account for being at quota */
  1239. if (ceph_osdmap_flag(&fsc->client->osdc, CEPH_OSDMAP_FULL)) {
  1240. err = -ENOSPC;
  1241. goto out;
  1242. }
  1243. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  1244. inode, ceph_vinop(inode), pos, count, i_size_read(inode));
  1245. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1246. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1247. else
  1248. want = CEPH_CAP_FILE_BUFFER;
  1249. got = 0;
  1250. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, pos + count,
  1251. &got, NULL);
  1252. if (err < 0)
  1253. goto out;
  1254. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  1255. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  1256. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  1257. (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC) ||
  1258. (ci->i_ceph_flags & CEPH_I_ERROR_WRITE)) {
  1259. struct ceph_snap_context *snapc;
  1260. struct iov_iter data;
  1261. inode_unlock(inode);
  1262. spin_lock(&ci->i_ceph_lock);
  1263. if (__ceph_have_pending_cap_snap(ci)) {
  1264. struct ceph_cap_snap *capsnap =
  1265. list_last_entry(&ci->i_cap_snaps,
  1266. struct ceph_cap_snap,
  1267. ci_item);
  1268. snapc = ceph_get_snap_context(capsnap->context);
  1269. } else {
  1270. BUG_ON(!ci->i_head_snapc);
  1271. snapc = ceph_get_snap_context(ci->i_head_snapc);
  1272. }
  1273. spin_unlock(&ci->i_ceph_lock);
  1274. /* we might need to revert back to that point */
  1275. data = *from;
  1276. if (iocb->ki_flags & IOCB_DIRECT)
  1277. written = ceph_direct_read_write(iocb, &data, snapc,
  1278. &prealloc_cf);
  1279. else
  1280. written = ceph_sync_write(iocb, &data, pos, snapc);
  1281. if (written > 0)
  1282. iov_iter_advance(from, written);
  1283. ceph_put_snap_context(snapc);
  1284. } else {
  1285. /*
  1286. * No need to acquire the i_truncate_mutex. Because
  1287. * the MDS revokes Fwb caps before sending truncate
  1288. * message to us. We can't get Fwb cap while there
  1289. * are pending vmtruncate. So write and vmtruncate
  1290. * can not run at the same time
  1291. */
  1292. written = generic_perform_write(file, from, pos);
  1293. if (likely(written >= 0))
  1294. iocb->ki_pos = pos + written;
  1295. inode_unlock(inode);
  1296. }
  1297. if (written >= 0) {
  1298. int dirty;
  1299. spin_lock(&ci->i_ceph_lock);
  1300. ci->i_inline_version = CEPH_INLINE_NONE;
  1301. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1302. &prealloc_cf);
  1303. spin_unlock(&ci->i_ceph_lock);
  1304. if (dirty)
  1305. __mark_inode_dirty(inode, dirty);
  1306. if (ceph_quota_is_max_bytes_approaching(inode, iocb->ki_pos))
  1307. ceph_check_caps(ci, CHECK_CAPS_NODELAY, NULL);
  1308. }
  1309. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  1310. inode, ceph_vinop(inode), pos, (unsigned)count,
  1311. ceph_cap_string(got));
  1312. ceph_put_cap_refs(ci, got);
  1313. if (written == -EOLDSNAPC) {
  1314. dout("aio_write %p %llx.%llx %llu~%u" "got EOLDSNAPC, retrying\n",
  1315. inode, ceph_vinop(inode), pos, (unsigned)count);
  1316. goto retry_snap;
  1317. }
  1318. if (written >= 0) {
  1319. if (ceph_osdmap_flag(&fsc->client->osdc, CEPH_OSDMAP_NEARFULL))
  1320. iocb->ki_flags |= IOCB_DSYNC;
  1321. written = generic_write_sync(iocb, written);
  1322. }
  1323. goto out_unlocked;
  1324. out:
  1325. inode_unlock(inode);
  1326. out_unlocked:
  1327. ceph_free_cap_flush(prealloc_cf);
  1328. current->backing_dev_info = NULL;
  1329. return written ? written : err;
  1330. }
  1331. /*
  1332. * llseek. be sure to verify file size on SEEK_END.
  1333. */
  1334. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  1335. {
  1336. struct inode *inode = file->f_mapping->host;
  1337. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1338. loff_t i_size;
  1339. loff_t ret;
  1340. inode_lock(inode);
  1341. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  1342. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  1343. if (ret < 0)
  1344. goto out;
  1345. }
  1346. i_size = i_size_read(inode);
  1347. switch (whence) {
  1348. case SEEK_END:
  1349. offset += i_size;
  1350. break;
  1351. case SEEK_CUR:
  1352. /*
  1353. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  1354. * position-querying operation. Avoid rewriting the "same"
  1355. * f_pos value back to the file because a concurrent read(),
  1356. * write() or lseek() might have altered it
  1357. */
  1358. if (offset == 0) {
  1359. ret = file->f_pos;
  1360. goto out;
  1361. }
  1362. offset += file->f_pos;
  1363. break;
  1364. case SEEK_DATA:
  1365. if (offset < 0 || offset >= i_size) {
  1366. ret = -ENXIO;
  1367. goto out;
  1368. }
  1369. break;
  1370. case SEEK_HOLE:
  1371. if (offset < 0 || offset >= i_size) {
  1372. ret = -ENXIO;
  1373. goto out;
  1374. }
  1375. offset = i_size;
  1376. break;
  1377. }
  1378. ret = vfs_setpos(file, offset, max(i_size, fsc->max_file_size));
  1379. out:
  1380. inode_unlock(inode);
  1381. return ret;
  1382. }
  1383. static inline void ceph_zero_partial_page(
  1384. struct inode *inode, loff_t offset, unsigned size)
  1385. {
  1386. struct page *page;
  1387. pgoff_t index = offset >> PAGE_SHIFT;
  1388. page = find_lock_page(inode->i_mapping, index);
  1389. if (page) {
  1390. wait_on_page_writeback(page);
  1391. zero_user(page, offset & (PAGE_SIZE - 1), size);
  1392. unlock_page(page);
  1393. put_page(page);
  1394. }
  1395. }
  1396. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  1397. loff_t length)
  1398. {
  1399. loff_t nearly = round_up(offset, PAGE_SIZE);
  1400. if (offset < nearly) {
  1401. loff_t size = nearly - offset;
  1402. if (length < size)
  1403. size = length;
  1404. ceph_zero_partial_page(inode, offset, size);
  1405. offset += size;
  1406. length -= size;
  1407. }
  1408. if (length >= PAGE_SIZE) {
  1409. loff_t size = round_down(length, PAGE_SIZE);
  1410. truncate_pagecache_range(inode, offset, offset + size - 1);
  1411. offset += size;
  1412. length -= size;
  1413. }
  1414. if (length)
  1415. ceph_zero_partial_page(inode, offset, length);
  1416. }
  1417. static int ceph_zero_partial_object(struct inode *inode,
  1418. loff_t offset, loff_t *length)
  1419. {
  1420. struct ceph_inode_info *ci = ceph_inode(inode);
  1421. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1422. struct ceph_osd_request *req;
  1423. int ret = 0;
  1424. loff_t zero = 0;
  1425. int op;
  1426. if (!length) {
  1427. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  1428. length = &zero;
  1429. } else {
  1430. op = CEPH_OSD_OP_ZERO;
  1431. }
  1432. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1433. ceph_vino(inode),
  1434. offset, length,
  1435. 0, 1, op,
  1436. CEPH_OSD_FLAG_WRITE,
  1437. NULL, 0, 0, false);
  1438. if (IS_ERR(req)) {
  1439. ret = PTR_ERR(req);
  1440. goto out;
  1441. }
  1442. req->r_mtime = inode->i_mtime;
  1443. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1444. if (!ret) {
  1445. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1446. if (ret == -ENOENT)
  1447. ret = 0;
  1448. }
  1449. ceph_osdc_put_request(req);
  1450. out:
  1451. return ret;
  1452. }
  1453. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  1454. {
  1455. int ret = 0;
  1456. struct ceph_inode_info *ci = ceph_inode(inode);
  1457. s32 stripe_unit = ci->i_layout.stripe_unit;
  1458. s32 stripe_count = ci->i_layout.stripe_count;
  1459. s32 object_size = ci->i_layout.object_size;
  1460. u64 object_set_size = object_size * stripe_count;
  1461. u64 nearly, t;
  1462. /* round offset up to next period boundary */
  1463. nearly = offset + object_set_size - 1;
  1464. t = nearly;
  1465. nearly -= do_div(t, object_set_size);
  1466. while (length && offset < nearly) {
  1467. loff_t size = length;
  1468. ret = ceph_zero_partial_object(inode, offset, &size);
  1469. if (ret < 0)
  1470. return ret;
  1471. offset += size;
  1472. length -= size;
  1473. }
  1474. while (length >= object_set_size) {
  1475. int i;
  1476. loff_t pos = offset;
  1477. for (i = 0; i < stripe_count; ++i) {
  1478. ret = ceph_zero_partial_object(inode, pos, NULL);
  1479. if (ret < 0)
  1480. return ret;
  1481. pos += stripe_unit;
  1482. }
  1483. offset += object_set_size;
  1484. length -= object_set_size;
  1485. }
  1486. while (length) {
  1487. loff_t size = length;
  1488. ret = ceph_zero_partial_object(inode, offset, &size);
  1489. if (ret < 0)
  1490. return ret;
  1491. offset += size;
  1492. length -= size;
  1493. }
  1494. return ret;
  1495. }
  1496. static long ceph_fallocate(struct file *file, int mode,
  1497. loff_t offset, loff_t length)
  1498. {
  1499. struct ceph_file_info *fi = file->private_data;
  1500. struct inode *inode = file_inode(file);
  1501. struct ceph_inode_info *ci = ceph_inode(inode);
  1502. struct ceph_cap_flush *prealloc_cf;
  1503. int want, got = 0;
  1504. int dirty;
  1505. int ret = 0;
  1506. loff_t endoff = 0;
  1507. loff_t size;
  1508. if (mode != (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1509. return -EOPNOTSUPP;
  1510. if (!S_ISREG(inode->i_mode))
  1511. return -EOPNOTSUPP;
  1512. prealloc_cf = ceph_alloc_cap_flush();
  1513. if (!prealloc_cf)
  1514. return -ENOMEM;
  1515. inode_lock(inode);
  1516. if (ceph_snap(inode) != CEPH_NOSNAP) {
  1517. ret = -EROFS;
  1518. goto unlock;
  1519. }
  1520. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1521. ret = ceph_uninline_data(file, NULL);
  1522. if (ret < 0)
  1523. goto unlock;
  1524. }
  1525. size = i_size_read(inode);
  1526. /* Are we punching a hole beyond EOF? */
  1527. if (offset >= size)
  1528. goto unlock;
  1529. if ((offset + length) > size)
  1530. length = size - offset;
  1531. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1532. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1533. else
  1534. want = CEPH_CAP_FILE_BUFFER;
  1535. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, endoff, &got, NULL);
  1536. if (ret < 0)
  1537. goto unlock;
  1538. ceph_zero_pagecache_range(inode, offset, length);
  1539. ret = ceph_zero_objects(inode, offset, length);
  1540. if (!ret) {
  1541. spin_lock(&ci->i_ceph_lock);
  1542. ci->i_inline_version = CEPH_INLINE_NONE;
  1543. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1544. &prealloc_cf);
  1545. spin_unlock(&ci->i_ceph_lock);
  1546. if (dirty)
  1547. __mark_inode_dirty(inode, dirty);
  1548. }
  1549. ceph_put_cap_refs(ci, got);
  1550. unlock:
  1551. inode_unlock(inode);
  1552. ceph_free_cap_flush(prealloc_cf);
  1553. return ret;
  1554. }
  1555. /*
  1556. * This function tries to get FILE_WR capabilities for dst_ci and FILE_RD for
  1557. * src_ci. Two attempts are made to obtain both caps, and an error is return if
  1558. * this fails; zero is returned on success.
  1559. */
  1560. static int get_rd_wr_caps(struct ceph_inode_info *src_ci,
  1561. loff_t src_endoff, int *src_got,
  1562. struct ceph_inode_info *dst_ci,
  1563. loff_t dst_endoff, int *dst_got)
  1564. {
  1565. int ret = 0;
  1566. bool retrying = false;
  1567. retry_caps:
  1568. ret = ceph_get_caps(dst_ci, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER,
  1569. dst_endoff, dst_got, NULL);
  1570. if (ret < 0)
  1571. return ret;
  1572. /*
  1573. * Since we're already holding the FILE_WR capability for the dst file,
  1574. * we would risk a deadlock by using ceph_get_caps. Thus, we'll do some
  1575. * retry dance instead to try to get both capabilities.
  1576. */
  1577. ret = ceph_try_get_caps(src_ci, CEPH_CAP_FILE_RD, CEPH_CAP_FILE_SHARED,
  1578. false, src_got);
  1579. if (ret <= 0) {
  1580. /* Start by dropping dst_ci caps and getting src_ci caps */
  1581. ceph_put_cap_refs(dst_ci, *dst_got);
  1582. if (retrying) {
  1583. if (!ret)
  1584. /* ceph_try_get_caps masks EAGAIN */
  1585. ret = -EAGAIN;
  1586. return ret;
  1587. }
  1588. ret = ceph_get_caps(src_ci, CEPH_CAP_FILE_RD,
  1589. CEPH_CAP_FILE_SHARED, src_endoff,
  1590. src_got, NULL);
  1591. if (ret < 0)
  1592. return ret;
  1593. /*... drop src_ci caps too, and retry */
  1594. ceph_put_cap_refs(src_ci, *src_got);
  1595. retrying = true;
  1596. goto retry_caps;
  1597. }
  1598. return ret;
  1599. }
  1600. static void put_rd_wr_caps(struct ceph_inode_info *src_ci, int src_got,
  1601. struct ceph_inode_info *dst_ci, int dst_got)
  1602. {
  1603. ceph_put_cap_refs(src_ci, src_got);
  1604. ceph_put_cap_refs(dst_ci, dst_got);
  1605. }
  1606. /*
  1607. * This function does several size-related checks, returning an error if:
  1608. * - source file is smaller than off+len
  1609. * - destination file size is not OK (inode_newsize_ok())
  1610. * - max bytes quotas is exceeded
  1611. */
  1612. static int is_file_size_ok(struct inode *src_inode, struct inode *dst_inode,
  1613. loff_t src_off, loff_t dst_off, size_t len)
  1614. {
  1615. loff_t size, endoff;
  1616. size = i_size_read(src_inode);
  1617. /*
  1618. * Don't copy beyond source file EOF. Instead of simply setting length
  1619. * to (size - src_off), just drop to VFS default implementation, as the
  1620. * local i_size may be stale due to other clients writing to the source
  1621. * inode.
  1622. */
  1623. if (src_off + len > size) {
  1624. dout("Copy beyond EOF (%llu + %zu > %llu)\n",
  1625. src_off, len, size);
  1626. return -EOPNOTSUPP;
  1627. }
  1628. size = i_size_read(dst_inode);
  1629. endoff = dst_off + len;
  1630. if (inode_newsize_ok(dst_inode, endoff))
  1631. return -EOPNOTSUPP;
  1632. if (ceph_quota_is_max_bytes_exceeded(dst_inode, endoff))
  1633. return -EDQUOT;
  1634. return 0;
  1635. }
  1636. static ssize_t ceph_copy_file_range(struct file *src_file, loff_t src_off,
  1637. struct file *dst_file, loff_t dst_off,
  1638. size_t len, unsigned int flags)
  1639. {
  1640. struct inode *src_inode = file_inode(src_file);
  1641. struct inode *dst_inode = file_inode(dst_file);
  1642. struct ceph_inode_info *src_ci = ceph_inode(src_inode);
  1643. struct ceph_inode_info *dst_ci = ceph_inode(dst_inode);
  1644. struct ceph_cap_flush *prealloc_cf;
  1645. struct ceph_object_locator src_oloc, dst_oloc;
  1646. struct ceph_object_id src_oid, dst_oid;
  1647. loff_t endoff = 0, size;
  1648. ssize_t ret = -EIO;
  1649. u64 src_objnum, dst_objnum, src_objoff, dst_objoff;
  1650. u32 src_objlen, dst_objlen, object_size;
  1651. int src_got = 0, dst_got = 0, err, dirty;
  1652. bool do_final_copy = false;
  1653. if (src_inode == dst_inode)
  1654. return -EINVAL;
  1655. if (ceph_snap(dst_inode) != CEPH_NOSNAP)
  1656. return -EROFS;
  1657. /*
  1658. * Some of the checks below will return -EOPNOTSUPP, which will force a
  1659. * fallback to the default VFS copy_file_range implementation. This is
  1660. * desirable in several cases (for ex, the 'len' is smaller than the
  1661. * size of the objects, or in cases where that would be more
  1662. * efficient).
  1663. */
  1664. if (ceph_test_mount_opt(ceph_inode_to_client(src_inode), NOCOPYFROM))
  1665. return -EOPNOTSUPP;
  1666. if ((src_ci->i_layout.stripe_unit != dst_ci->i_layout.stripe_unit) ||
  1667. (src_ci->i_layout.stripe_count != dst_ci->i_layout.stripe_count) ||
  1668. (src_ci->i_layout.object_size != dst_ci->i_layout.object_size))
  1669. return -EOPNOTSUPP;
  1670. if (len < src_ci->i_layout.object_size)
  1671. return -EOPNOTSUPP; /* no remote copy will be done */
  1672. prealloc_cf = ceph_alloc_cap_flush();
  1673. if (!prealloc_cf)
  1674. return -ENOMEM;
  1675. /* Start by sync'ing the source and destination files */
  1676. ret = file_write_and_wait_range(src_file, src_off, (src_off + len));
  1677. if (ret < 0) {
  1678. dout("failed to write src file (%zd)\n", ret);
  1679. goto out;
  1680. }
  1681. ret = file_write_and_wait_range(dst_file, dst_off, (dst_off + len));
  1682. if (ret < 0) {
  1683. dout("failed to write dst file (%zd)\n", ret);
  1684. goto out;
  1685. }
  1686. /*
  1687. * We need FILE_WR caps for dst_ci and FILE_RD for src_ci as other
  1688. * clients may have dirty data in their caches. And OSDs know nothing
  1689. * about caps, so they can't safely do the remote object copies.
  1690. */
  1691. err = get_rd_wr_caps(src_ci, (src_off + len), &src_got,
  1692. dst_ci, (dst_off + len), &dst_got);
  1693. if (err < 0) {
  1694. dout("get_rd_wr_caps returned %d\n", err);
  1695. ret = -EOPNOTSUPP;
  1696. goto out;
  1697. }
  1698. ret = is_file_size_ok(src_inode, dst_inode, src_off, dst_off, len);
  1699. if (ret < 0)
  1700. goto out_caps;
  1701. size = i_size_read(dst_inode);
  1702. endoff = dst_off + len;
  1703. /* Drop dst file cached pages */
  1704. ret = invalidate_inode_pages2_range(dst_inode->i_mapping,
  1705. dst_off >> PAGE_SHIFT,
  1706. endoff >> PAGE_SHIFT);
  1707. if (ret < 0) {
  1708. dout("Failed to invalidate inode pages (%zd)\n", ret);
  1709. ret = 0; /* XXX */
  1710. }
  1711. src_oloc.pool = src_ci->i_layout.pool_id;
  1712. src_oloc.pool_ns = ceph_try_get_string(src_ci->i_layout.pool_ns);
  1713. dst_oloc.pool = dst_ci->i_layout.pool_id;
  1714. dst_oloc.pool_ns = ceph_try_get_string(dst_ci->i_layout.pool_ns);
  1715. ceph_calc_file_object_mapping(&src_ci->i_layout, src_off,
  1716. src_ci->i_layout.object_size,
  1717. &src_objnum, &src_objoff, &src_objlen);
  1718. ceph_calc_file_object_mapping(&dst_ci->i_layout, dst_off,
  1719. dst_ci->i_layout.object_size,
  1720. &dst_objnum, &dst_objoff, &dst_objlen);
  1721. /* object-level offsets need to the same */
  1722. if (src_objoff != dst_objoff) {
  1723. ret = -EOPNOTSUPP;
  1724. goto out_caps;
  1725. }
  1726. /*
  1727. * Do a manual copy if the object offset isn't object aligned.
  1728. * 'src_objlen' contains the bytes left until the end of the object,
  1729. * starting at the src_off
  1730. */
  1731. if (src_objoff) {
  1732. /*
  1733. * we need to temporarily drop all caps as we'll be calling
  1734. * {read,write}_iter, which will get caps again.
  1735. */
  1736. put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
  1737. ret = do_splice_direct(src_file, &src_off, dst_file,
  1738. &dst_off, src_objlen, flags);
  1739. if (ret < 0) {
  1740. dout("do_splice_direct returned %d\n", err);
  1741. goto out;
  1742. }
  1743. len -= ret;
  1744. err = get_rd_wr_caps(src_ci, (src_off + len),
  1745. &src_got, dst_ci,
  1746. (dst_off + len), &dst_got);
  1747. if (err < 0)
  1748. goto out;
  1749. err = is_file_size_ok(src_inode, dst_inode,
  1750. src_off, dst_off, len);
  1751. if (err < 0)
  1752. goto out_caps;
  1753. }
  1754. object_size = src_ci->i_layout.object_size;
  1755. while (len >= object_size) {
  1756. ceph_calc_file_object_mapping(&src_ci->i_layout, src_off,
  1757. object_size, &src_objnum,
  1758. &src_objoff, &src_objlen);
  1759. ceph_calc_file_object_mapping(&dst_ci->i_layout, dst_off,
  1760. object_size, &dst_objnum,
  1761. &dst_objoff, &dst_objlen);
  1762. ceph_oid_init(&src_oid);
  1763. ceph_oid_printf(&src_oid, "%llx.%08llx",
  1764. src_ci->i_vino.ino, src_objnum);
  1765. ceph_oid_init(&dst_oid);
  1766. ceph_oid_printf(&dst_oid, "%llx.%08llx",
  1767. dst_ci->i_vino.ino, dst_objnum);
  1768. /* Do an object remote copy */
  1769. err = ceph_osdc_copy_from(
  1770. &ceph_inode_to_client(src_inode)->client->osdc,
  1771. src_ci->i_vino.snap, 0,
  1772. &src_oid, &src_oloc,
  1773. CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
  1774. CEPH_OSD_OP_FLAG_FADVISE_NOCACHE,
  1775. &dst_oid, &dst_oloc,
  1776. CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
  1777. CEPH_OSD_OP_FLAG_FADVISE_DONTNEED, 0);
  1778. if (err) {
  1779. dout("ceph_osdc_copy_from returned %d\n", err);
  1780. if (!ret)
  1781. ret = err;
  1782. goto out_caps;
  1783. }
  1784. len -= object_size;
  1785. src_off += object_size;
  1786. dst_off += object_size;
  1787. ret += object_size;
  1788. }
  1789. if (len)
  1790. /* We still need one final local copy */
  1791. do_final_copy = true;
  1792. file_update_time(dst_file);
  1793. if (endoff > size) {
  1794. int caps_flags = 0;
  1795. /* Let the MDS know about dst file size change */
  1796. if (ceph_quota_is_max_bytes_approaching(dst_inode, endoff))
  1797. caps_flags |= CHECK_CAPS_NODELAY;
  1798. if (ceph_inode_set_size(dst_inode, endoff))
  1799. caps_flags |= CHECK_CAPS_AUTHONLY;
  1800. if (caps_flags)
  1801. ceph_check_caps(dst_ci, caps_flags, NULL);
  1802. }
  1803. /* Mark Fw dirty */
  1804. spin_lock(&dst_ci->i_ceph_lock);
  1805. dst_ci->i_inline_version = CEPH_INLINE_NONE;
  1806. dirty = __ceph_mark_dirty_caps(dst_ci, CEPH_CAP_FILE_WR, &prealloc_cf);
  1807. spin_unlock(&dst_ci->i_ceph_lock);
  1808. if (dirty)
  1809. __mark_inode_dirty(dst_inode, dirty);
  1810. out_caps:
  1811. put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
  1812. if (do_final_copy) {
  1813. err = do_splice_direct(src_file, &src_off, dst_file,
  1814. &dst_off, len, flags);
  1815. if (err < 0) {
  1816. dout("do_splice_direct returned %d\n", err);
  1817. goto out;
  1818. }
  1819. len -= err;
  1820. ret += err;
  1821. }
  1822. out:
  1823. ceph_free_cap_flush(prealloc_cf);
  1824. return ret;
  1825. }
  1826. const struct file_operations ceph_file_fops = {
  1827. .open = ceph_open,
  1828. .release = ceph_release,
  1829. .llseek = ceph_llseek,
  1830. .read_iter = ceph_read_iter,
  1831. .write_iter = ceph_write_iter,
  1832. .mmap = ceph_mmap,
  1833. .fsync = ceph_fsync,
  1834. .lock = ceph_lock,
  1835. .flock = ceph_flock,
  1836. .splice_read = generic_file_splice_read,
  1837. .splice_write = iter_file_splice_write,
  1838. .unlocked_ioctl = ceph_ioctl,
  1839. .compat_ioctl = ceph_ioctl,
  1840. .fallocate = ceph_fallocate,
  1841. .copy_file_range = ceph_copy_file_range,
  1842. };