file.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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/aio.h>
  10. #include <linux/falloc.h>
  11. #include "super.h"
  12. #include "mds_client.h"
  13. #include "cache.h"
  14. /*
  15. * Ceph file operations
  16. *
  17. * Implement basic open/close functionality, and implement
  18. * read/write.
  19. *
  20. * We implement three modes of file I/O:
  21. * - buffered uses the generic_file_aio_{read,write} helpers
  22. *
  23. * - synchronous is used when there is multi-client read/write
  24. * sharing, avoids the page cache, and synchronously waits for an
  25. * ack from the OSD.
  26. *
  27. * - direct io takes the variant of the sync path that references
  28. * user pages directly.
  29. *
  30. * fsync() flushes and waits on dirty pages, but just queues metadata
  31. * for writeback: since the MDS can recover size and mtime there is no
  32. * need to wait for MDS acknowledgement.
  33. */
  34. /*
  35. * Prepare an open request. Preallocate ceph_cap to avoid an
  36. * inopportune ENOMEM later.
  37. */
  38. static struct ceph_mds_request *
  39. prepare_open_request(struct super_block *sb, int flags, int create_mode)
  40. {
  41. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  42. struct ceph_mds_client *mdsc = fsc->mdsc;
  43. struct ceph_mds_request *req;
  44. int want_auth = USE_ANY_MDS;
  45. int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
  46. if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
  47. want_auth = USE_AUTH_MDS;
  48. req = ceph_mdsc_create_request(mdsc, op, want_auth);
  49. if (IS_ERR(req))
  50. goto out;
  51. req->r_fmode = ceph_flags_to_mode(flags);
  52. req->r_args.open.flags = cpu_to_le32(flags);
  53. req->r_args.open.mode = cpu_to_le32(create_mode);
  54. out:
  55. return req;
  56. }
  57. /*
  58. * initialize private struct file data.
  59. * if we fail, clean up by dropping fmode reference on the ceph_inode
  60. */
  61. static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
  62. {
  63. struct ceph_file_info *cf;
  64. int ret = 0;
  65. struct ceph_inode_info *ci = ceph_inode(inode);
  66. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  67. struct ceph_mds_client *mdsc = fsc->mdsc;
  68. switch (inode->i_mode & S_IFMT) {
  69. case S_IFREG:
  70. /* First file open request creates the cookie, we want to keep
  71. * this cookie around for the filetime of the inode as not to
  72. * have to worry about fscache register / revoke / operation
  73. * races.
  74. *
  75. * Also, if we know the operation is going to invalidate data
  76. * (non readonly) just nuke the cache right away.
  77. */
  78. ceph_fscache_register_inode_cookie(mdsc->fsc, ci);
  79. if ((fmode & CEPH_FILE_MODE_WR))
  80. ceph_fscache_invalidate(inode);
  81. case S_IFDIR:
  82. dout("init_file %p %p 0%o (regular)\n", inode, file,
  83. inode->i_mode);
  84. cf = kmem_cache_alloc(ceph_file_cachep, GFP_NOFS | __GFP_ZERO);
  85. if (cf == NULL) {
  86. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  87. return -ENOMEM;
  88. }
  89. cf->fmode = fmode;
  90. cf->next_offset = 2;
  91. file->private_data = cf;
  92. BUG_ON(inode->i_fop->release != ceph_release);
  93. break;
  94. case S_IFLNK:
  95. dout("init_file %p %p 0%o (symlink)\n", inode, file,
  96. inode->i_mode);
  97. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  98. break;
  99. default:
  100. dout("init_file %p %p 0%o (special)\n", inode, file,
  101. inode->i_mode);
  102. /*
  103. * we need to drop the open ref now, since we don't
  104. * have .release set to ceph_release.
  105. */
  106. ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
  107. BUG_ON(inode->i_fop->release == ceph_release);
  108. /* call the proper open fop */
  109. ret = inode->i_fop->open(inode, file);
  110. }
  111. return ret;
  112. }
  113. /*
  114. * If we already have the requisite capabilities, we can satisfy
  115. * the open request locally (no need to request new caps from the
  116. * MDS). We do, however, need to inform the MDS (asynchronously)
  117. * if our wanted caps set expands.
  118. */
  119. int ceph_open(struct inode *inode, struct file *file)
  120. {
  121. struct ceph_inode_info *ci = ceph_inode(inode);
  122. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  123. struct ceph_mds_client *mdsc = fsc->mdsc;
  124. struct ceph_mds_request *req;
  125. struct ceph_file_info *cf = file->private_data;
  126. struct inode *parent_inode = NULL;
  127. int err;
  128. int flags, fmode, wanted;
  129. if (cf) {
  130. dout("open file %p is already opened\n", file);
  131. return 0;
  132. }
  133. /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
  134. flags = file->f_flags & ~(O_CREAT|O_EXCL);
  135. if (S_ISDIR(inode->i_mode))
  136. flags = O_DIRECTORY; /* mds likes to know */
  137. dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
  138. ceph_vinop(inode), file, flags, file->f_flags);
  139. fmode = ceph_flags_to_mode(flags);
  140. wanted = ceph_caps_for_mode(fmode);
  141. /* snapped files are read-only */
  142. if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
  143. return -EROFS;
  144. /* trivially open snapdir */
  145. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  146. spin_lock(&ci->i_ceph_lock);
  147. __ceph_get_fmode(ci, fmode);
  148. spin_unlock(&ci->i_ceph_lock);
  149. return ceph_init_file(inode, file, fmode);
  150. }
  151. /*
  152. * No need to block if we have caps on the auth MDS (for
  153. * write) or any MDS (for read). Update wanted set
  154. * asynchronously.
  155. */
  156. spin_lock(&ci->i_ceph_lock);
  157. if (__ceph_is_any_real_caps(ci) &&
  158. (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
  159. int mds_wanted = __ceph_caps_mds_wanted(ci);
  160. int issued = __ceph_caps_issued(ci, NULL);
  161. dout("open %p fmode %d want %s issued %s using existing\n",
  162. inode, fmode, ceph_cap_string(wanted),
  163. ceph_cap_string(issued));
  164. __ceph_get_fmode(ci, fmode);
  165. spin_unlock(&ci->i_ceph_lock);
  166. /* adjust wanted? */
  167. if ((issued & wanted) != wanted &&
  168. (mds_wanted & wanted) != wanted &&
  169. ceph_snap(inode) != CEPH_SNAPDIR)
  170. ceph_check_caps(ci, 0, NULL);
  171. return ceph_init_file(inode, file, fmode);
  172. } else if (ceph_snap(inode) != CEPH_NOSNAP &&
  173. (ci->i_snap_caps & wanted) == wanted) {
  174. __ceph_get_fmode(ci, fmode);
  175. spin_unlock(&ci->i_ceph_lock);
  176. return ceph_init_file(inode, file, fmode);
  177. }
  178. spin_unlock(&ci->i_ceph_lock);
  179. dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
  180. req = prepare_open_request(inode->i_sb, flags, 0);
  181. if (IS_ERR(req)) {
  182. err = PTR_ERR(req);
  183. goto out;
  184. }
  185. req->r_inode = inode;
  186. ihold(inode);
  187. req->r_num_caps = 1;
  188. if (flags & (O_CREAT|O_TRUNC))
  189. parent_inode = ceph_get_dentry_parent_inode(file->f_dentry);
  190. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  191. iput(parent_inode);
  192. if (!err)
  193. err = ceph_init_file(inode, file, req->r_fmode);
  194. ceph_mdsc_put_request(req);
  195. dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
  196. out:
  197. return err;
  198. }
  199. /*
  200. * Do a lookup + open with a single request. If we get a non-existent
  201. * file or symlink, return 1 so the VFS can retry.
  202. */
  203. int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
  204. struct file *file, unsigned flags, umode_t mode,
  205. int *opened)
  206. {
  207. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  208. struct ceph_mds_client *mdsc = fsc->mdsc;
  209. struct ceph_mds_request *req;
  210. struct dentry *dn;
  211. int err;
  212. dout("atomic_open %p dentry %p '%.*s' %s flags %d mode 0%o\n",
  213. dir, dentry, dentry->d_name.len, dentry->d_name.name,
  214. d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
  215. if (dentry->d_name.len > NAME_MAX)
  216. return -ENAMETOOLONG;
  217. err = ceph_init_dentry(dentry);
  218. if (err < 0)
  219. return err;
  220. /* do the open */
  221. req = prepare_open_request(dir->i_sb, flags, mode);
  222. if (IS_ERR(req))
  223. return PTR_ERR(req);
  224. req->r_dentry = dget(dentry);
  225. req->r_num_caps = 2;
  226. if (flags & O_CREAT) {
  227. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  228. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  229. }
  230. req->r_locked_dir = dir; /* caller holds dir->i_mutex */
  231. err = ceph_mdsc_do_request(mdsc,
  232. (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
  233. req);
  234. if (err)
  235. goto out_err;
  236. err = ceph_handle_snapdir(req, dentry, err);
  237. if (err == 0 && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  238. err = ceph_handle_notrace_create(dir, dentry);
  239. if (d_unhashed(dentry)) {
  240. dn = ceph_finish_lookup(req, dentry, err);
  241. if (IS_ERR(dn))
  242. err = PTR_ERR(dn);
  243. } else {
  244. /* we were given a hashed negative dentry */
  245. dn = NULL;
  246. }
  247. if (err)
  248. goto out_err;
  249. if (dn || dentry->d_inode == NULL || S_ISLNK(dentry->d_inode->i_mode)) {
  250. /* make vfs retry on splice, ENOENT, or symlink */
  251. dout("atomic_open finish_no_open on dn %p\n", dn);
  252. err = finish_no_open(file, dn);
  253. } else {
  254. dout("atomic_open finish_open on dn %p\n", dn);
  255. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  256. *opened |= FILE_CREATED;
  257. }
  258. err = finish_open(file, dentry, ceph_open, opened);
  259. }
  260. out_err:
  261. ceph_mdsc_put_request(req);
  262. dout("atomic_open result=%d\n", err);
  263. return err;
  264. }
  265. int ceph_release(struct inode *inode, struct file *file)
  266. {
  267. struct ceph_inode_info *ci = ceph_inode(inode);
  268. struct ceph_file_info *cf = file->private_data;
  269. dout("release inode %p file %p\n", inode, file);
  270. ceph_put_fmode(ci, cf->fmode);
  271. if (cf->last_readdir)
  272. ceph_mdsc_put_request(cf->last_readdir);
  273. kfree(cf->last_name);
  274. kfree(cf->dir_info);
  275. dput(cf->dentry);
  276. kmem_cache_free(ceph_file_cachep, cf);
  277. /* wake up anyone waiting for caps on this inode */
  278. wake_up_all(&ci->i_cap_wq);
  279. return 0;
  280. }
  281. /*
  282. * Read a range of bytes striped over one or more objects. Iterate over
  283. * objects we stripe over. (That's not atomic, but good enough for now.)
  284. *
  285. * If we get a short result from the OSD, check against i_size; we need to
  286. * only return a short read to the caller if we hit EOF.
  287. */
  288. static int striped_read(struct inode *inode,
  289. u64 off, u64 len,
  290. struct page **pages, int num_pages,
  291. int *checkeof, bool o_direct,
  292. unsigned long buf_align)
  293. {
  294. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  295. struct ceph_inode_info *ci = ceph_inode(inode);
  296. u64 pos, this_len, left;
  297. int io_align, page_align;
  298. int pages_left;
  299. int read;
  300. struct page **page_pos;
  301. int ret;
  302. bool hit_stripe, was_short;
  303. /*
  304. * we may need to do multiple reads. not atomic, unfortunately.
  305. */
  306. pos = off;
  307. left = len;
  308. page_pos = pages;
  309. pages_left = num_pages;
  310. read = 0;
  311. io_align = off & ~PAGE_MASK;
  312. more:
  313. if (o_direct)
  314. page_align = (pos - io_align + buf_align) & ~PAGE_MASK;
  315. else
  316. page_align = pos & ~PAGE_MASK;
  317. this_len = left;
  318. ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
  319. &ci->i_layout, pos, &this_len,
  320. ci->i_truncate_seq,
  321. ci->i_truncate_size,
  322. page_pos, pages_left, page_align);
  323. if (ret == -ENOENT)
  324. ret = 0;
  325. hit_stripe = this_len < left;
  326. was_short = ret >= 0 && ret < this_len;
  327. dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, left, read,
  328. ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
  329. if (ret >= 0) {
  330. int didpages;
  331. if (was_short && (pos + ret < inode->i_size)) {
  332. u64 tmp = min(this_len - ret,
  333. inode->i_size - pos - ret);
  334. dout(" zero gap %llu to %llu\n",
  335. pos + ret, pos + ret + tmp);
  336. ceph_zero_page_vector_range(page_align + read + ret,
  337. tmp, pages);
  338. ret += tmp;
  339. }
  340. didpages = (page_align + ret) >> PAGE_CACHE_SHIFT;
  341. pos += ret;
  342. read = pos - off;
  343. left -= ret;
  344. page_pos += didpages;
  345. pages_left -= didpages;
  346. /* hit stripe and need continue*/
  347. if (left && hit_stripe && pos < inode->i_size)
  348. goto more;
  349. }
  350. if (read > 0) {
  351. ret = read;
  352. /* did we bounce off eof? */
  353. if (pos + left > inode->i_size)
  354. *checkeof = 1;
  355. }
  356. dout("striped_read returns %d\n", ret);
  357. return ret;
  358. }
  359. /*
  360. * Completely synchronous read and write methods. Direct from __user
  361. * buffer to osd, or directly to user pages (if O_DIRECT).
  362. *
  363. * If the read spans object boundary, just do multiple reads.
  364. */
  365. static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *i,
  366. int *checkeof)
  367. {
  368. struct file *file = iocb->ki_filp;
  369. struct inode *inode = file_inode(file);
  370. struct page **pages;
  371. u64 off = iocb->ki_pos;
  372. int num_pages, ret;
  373. size_t len = i->count;
  374. dout("sync_read on file %p %llu~%u %s\n", file, off,
  375. (unsigned)len,
  376. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  377. /*
  378. * flush any page cache pages in this range. this
  379. * will make concurrent normal and sync io slow,
  380. * but it will at least behave sensibly when they are
  381. * in sequence.
  382. */
  383. ret = filemap_write_and_wait_range(inode->i_mapping, off,
  384. off + len);
  385. if (ret < 0)
  386. return ret;
  387. if (file->f_flags & O_DIRECT) {
  388. while (iov_iter_count(i)) {
  389. void __user *data = i->iov[0].iov_base + i->iov_offset;
  390. size_t len = i->iov[0].iov_len - i->iov_offset;
  391. num_pages = calc_pages_for((unsigned long)data, len);
  392. pages = ceph_get_direct_page_vector(data,
  393. num_pages, true);
  394. if (IS_ERR(pages))
  395. return PTR_ERR(pages);
  396. ret = striped_read(inode, off, len,
  397. pages, num_pages, checkeof,
  398. 1, (unsigned long)data & ~PAGE_MASK);
  399. ceph_put_page_vector(pages, num_pages, true);
  400. if (ret <= 0)
  401. break;
  402. off += ret;
  403. iov_iter_advance(i, ret);
  404. if (ret < len)
  405. break;
  406. }
  407. } else {
  408. num_pages = calc_pages_for(off, len);
  409. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  410. if (IS_ERR(pages))
  411. return PTR_ERR(pages);
  412. ret = striped_read(inode, off, len, pages,
  413. num_pages, checkeof, 0, 0);
  414. if (ret > 0) {
  415. int l, k = 0;
  416. size_t left = len = ret;
  417. while (left) {
  418. void __user *data = i->iov[0].iov_base
  419. + i->iov_offset;
  420. l = min(i->iov[0].iov_len - i->iov_offset,
  421. left);
  422. ret = ceph_copy_page_vector_to_user(&pages[k],
  423. data, off,
  424. l);
  425. if (ret > 0) {
  426. iov_iter_advance(i, ret);
  427. left -= ret;
  428. off += ret;
  429. k = calc_pages_for(iocb->ki_pos,
  430. len - left + 1) - 1;
  431. BUG_ON(k >= num_pages && left);
  432. } else
  433. break;
  434. }
  435. }
  436. ceph_release_page_vector(pages, num_pages);
  437. }
  438. if (off > iocb->ki_pos) {
  439. ret = off - iocb->ki_pos;
  440. iocb->ki_pos = off;
  441. }
  442. dout("sync_read result %d\n", ret);
  443. return ret;
  444. }
  445. /*
  446. * Write commit request unsafe callback, called to tell us when a
  447. * request is unsafe (that is, in flight--has been handed to the
  448. * messenger to send to its target osd). It is called again when
  449. * we've received a response message indicating the request is
  450. * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
  451. * is completed early (and unsuccessfully) due to a timeout or
  452. * interrupt.
  453. *
  454. * This is used if we requested both an ACK and ONDISK commit reply
  455. * from the OSD.
  456. */
  457. static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
  458. {
  459. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  460. dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
  461. unsafe ? "un" : "");
  462. if (unsafe) {
  463. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  464. spin_lock(&ci->i_unsafe_lock);
  465. list_add_tail(&req->r_unsafe_item,
  466. &ci->i_unsafe_writes);
  467. spin_unlock(&ci->i_unsafe_lock);
  468. } else {
  469. spin_lock(&ci->i_unsafe_lock);
  470. list_del_init(&req->r_unsafe_item);
  471. spin_unlock(&ci->i_unsafe_lock);
  472. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  473. }
  474. }
  475. /*
  476. * Synchronous write, straight from __user pointer or user pages.
  477. *
  478. * If write spans object boundary, just do multiple writes. (For a
  479. * correct atomic write, we should e.g. take write locks on all
  480. * objects, rollback on failure, etc.)
  481. */
  482. static ssize_t
  483. ceph_sync_direct_write(struct kiocb *iocb, const struct iovec *iov,
  484. unsigned long nr_segs, size_t count)
  485. {
  486. struct file *file = iocb->ki_filp;
  487. struct inode *inode = file_inode(file);
  488. struct ceph_inode_info *ci = ceph_inode(inode);
  489. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  490. struct ceph_snap_context *snapc;
  491. struct ceph_vino vino;
  492. struct ceph_osd_request *req;
  493. struct page **pages;
  494. int num_pages;
  495. int written = 0;
  496. int flags;
  497. int check_caps = 0;
  498. int page_align;
  499. int ret;
  500. struct timespec mtime = CURRENT_TIME;
  501. loff_t pos = iocb->ki_pos;
  502. struct iov_iter i;
  503. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  504. return -EROFS;
  505. dout("sync_direct_write on file %p %lld~%u\n", file, pos,
  506. (unsigned)count);
  507. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  508. if (ret < 0)
  509. return ret;
  510. ret = invalidate_inode_pages2_range(inode->i_mapping,
  511. pos >> PAGE_CACHE_SHIFT,
  512. (pos + count) >> PAGE_CACHE_SHIFT);
  513. if (ret < 0)
  514. dout("invalidate_inode_pages2_range returned %d\n", ret);
  515. flags = CEPH_OSD_FLAG_ORDERSNAP |
  516. CEPH_OSD_FLAG_ONDISK |
  517. CEPH_OSD_FLAG_WRITE;
  518. iov_iter_init(&i, iov, nr_segs, count, 0);
  519. while (iov_iter_count(&i) > 0) {
  520. void __user *data = i.iov->iov_base + i.iov_offset;
  521. u64 len = i.iov->iov_len - i.iov_offset;
  522. page_align = (unsigned long)data & ~PAGE_MASK;
  523. snapc = ci->i_snap_realm->cached_context;
  524. vino = ceph_vino(inode);
  525. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  526. vino, pos, &len,
  527. 2,/*include a 'startsync' command*/
  528. CEPH_OSD_OP_WRITE, flags, snapc,
  529. ci->i_truncate_seq,
  530. ci->i_truncate_size,
  531. false);
  532. if (IS_ERR(req)) {
  533. ret = PTR_ERR(req);
  534. goto out;
  535. }
  536. num_pages = calc_pages_for(page_align, len);
  537. pages = ceph_get_direct_page_vector(data, num_pages, false);
  538. if (IS_ERR(pages)) {
  539. ret = PTR_ERR(pages);
  540. goto out;
  541. }
  542. /*
  543. * throw out any page cache pages in this range. this
  544. * may block.
  545. */
  546. truncate_inode_pages_range(inode->i_mapping, pos,
  547. (pos+len) | (PAGE_CACHE_SIZE-1));
  548. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
  549. false, false);
  550. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  551. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  552. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  553. if (!ret)
  554. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  555. ceph_put_page_vector(pages, num_pages, false);
  556. out:
  557. ceph_osdc_put_request(req);
  558. if (ret == 0) {
  559. pos += len;
  560. written += len;
  561. iov_iter_advance(&i, (size_t)len);
  562. if (pos > i_size_read(inode)) {
  563. check_caps = ceph_inode_set_size(inode, pos);
  564. if (check_caps)
  565. ceph_check_caps(ceph_inode(inode),
  566. CHECK_CAPS_AUTHONLY,
  567. NULL);
  568. }
  569. } else
  570. break;
  571. }
  572. if (ret != -EOLDSNAPC && written > 0) {
  573. iocb->ki_pos = pos;
  574. ret = written;
  575. }
  576. return ret;
  577. }
  578. /*
  579. * Synchronous write, straight from __user pointer or user pages.
  580. *
  581. * If write spans object boundary, just do multiple writes. (For a
  582. * correct atomic write, we should e.g. take write locks on all
  583. * objects, rollback on failure, etc.)
  584. */
  585. static ssize_t ceph_sync_write(struct kiocb *iocb, const struct iovec *iov,
  586. unsigned long nr_segs, size_t count)
  587. {
  588. struct file *file = iocb->ki_filp;
  589. struct inode *inode = file_inode(file);
  590. struct ceph_inode_info *ci = ceph_inode(inode);
  591. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  592. struct ceph_snap_context *snapc;
  593. struct ceph_vino vino;
  594. struct ceph_osd_request *req;
  595. struct page **pages;
  596. u64 len;
  597. int num_pages;
  598. int written = 0;
  599. int flags;
  600. int check_caps = 0;
  601. int ret;
  602. struct timespec mtime = CURRENT_TIME;
  603. loff_t pos = iocb->ki_pos;
  604. struct iov_iter i;
  605. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  606. return -EROFS;
  607. dout("sync_write on file %p %lld~%u\n", file, pos, (unsigned)count);
  608. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
  609. if (ret < 0)
  610. return ret;
  611. ret = invalidate_inode_pages2_range(inode->i_mapping,
  612. pos >> PAGE_CACHE_SHIFT,
  613. (pos + count) >> PAGE_CACHE_SHIFT);
  614. if (ret < 0)
  615. dout("invalidate_inode_pages2_range returned %d\n", ret);
  616. flags = CEPH_OSD_FLAG_ORDERSNAP |
  617. CEPH_OSD_FLAG_ONDISK |
  618. CEPH_OSD_FLAG_WRITE |
  619. CEPH_OSD_FLAG_ACK;
  620. iov_iter_init(&i, iov, nr_segs, count, 0);
  621. while ((len = iov_iter_count(&i)) > 0) {
  622. size_t left;
  623. int n;
  624. snapc = ci->i_snap_realm->cached_context;
  625. vino = ceph_vino(inode);
  626. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  627. vino, pos, &len, 1,
  628. CEPH_OSD_OP_WRITE, flags, snapc,
  629. ci->i_truncate_seq,
  630. ci->i_truncate_size,
  631. false);
  632. if (IS_ERR(req)) {
  633. ret = PTR_ERR(req);
  634. goto out;
  635. }
  636. /*
  637. * write from beginning of first page,
  638. * regardless of io alignment
  639. */
  640. num_pages = (len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  641. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  642. if (IS_ERR(pages)) {
  643. ret = PTR_ERR(pages);
  644. goto out;
  645. }
  646. left = len;
  647. for (n = 0; n < num_pages; n++) {
  648. size_t plen = min_t(size_t, left, PAGE_SIZE);
  649. ret = iov_iter_copy_from_user(pages[n], &i, 0, plen);
  650. if (ret != plen) {
  651. ret = -EFAULT;
  652. break;
  653. }
  654. left -= ret;
  655. iov_iter_advance(&i, ret);
  656. }
  657. if (ret < 0) {
  658. ceph_release_page_vector(pages, num_pages);
  659. goto out;
  660. }
  661. /* get a second commit callback */
  662. req->r_unsafe_callback = ceph_sync_write_unsafe;
  663. req->r_inode = inode;
  664. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
  665. false, true);
  666. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  667. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  668. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  669. if (!ret)
  670. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  671. out:
  672. ceph_osdc_put_request(req);
  673. if (ret == 0) {
  674. pos += len;
  675. written += len;
  676. if (pos > i_size_read(inode)) {
  677. check_caps = ceph_inode_set_size(inode, pos);
  678. if (check_caps)
  679. ceph_check_caps(ceph_inode(inode),
  680. CHECK_CAPS_AUTHONLY,
  681. NULL);
  682. }
  683. } else
  684. break;
  685. }
  686. if (ret != -EOLDSNAPC && written > 0) {
  687. ret = written;
  688. iocb->ki_pos = pos;
  689. }
  690. return ret;
  691. }
  692. /*
  693. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  694. * Atomically grab references, so that those bits are not released
  695. * back to the MDS mid-read.
  696. *
  697. * Hmm, the sync read case isn't actually async... should it be?
  698. */
  699. static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov,
  700. unsigned long nr_segs, loff_t pos)
  701. {
  702. struct file *filp = iocb->ki_filp;
  703. struct ceph_file_info *fi = filp->private_data;
  704. size_t len = iocb->ki_nbytes;
  705. struct inode *inode = file_inode(filp);
  706. struct ceph_inode_info *ci = ceph_inode(inode);
  707. ssize_t ret;
  708. int want, got = 0;
  709. int checkeof = 0, read = 0;
  710. again:
  711. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  712. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
  713. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  714. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  715. else
  716. want = CEPH_CAP_FILE_CACHE;
  717. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, &got, -1);
  718. if (ret < 0)
  719. return ret;
  720. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  721. (iocb->ki_filp->f_flags & O_DIRECT) ||
  722. (fi->flags & CEPH_F_SYNC)) {
  723. struct iov_iter i;
  724. dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  725. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  726. ceph_cap_string(got));
  727. if (!read) {
  728. ret = generic_segment_checks(iov, &nr_segs,
  729. &len, VERIFY_WRITE);
  730. if (ret)
  731. goto out;
  732. }
  733. iov_iter_init(&i, iov, nr_segs, len, read);
  734. /* hmm, this isn't really async... */
  735. ret = ceph_sync_read(iocb, &i, &checkeof);
  736. } else {
  737. /*
  738. * We can't modify the content of iov,
  739. * so we only read from beginning.
  740. */
  741. if (read) {
  742. iocb->ki_pos = pos;
  743. len = iocb->ki_nbytes;
  744. read = 0;
  745. }
  746. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  747. inode, ceph_vinop(inode), pos, (unsigned)len,
  748. ceph_cap_string(got));
  749. ret = generic_file_aio_read(iocb, iov, nr_segs, pos);
  750. }
  751. out:
  752. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  753. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  754. ceph_put_cap_refs(ci, got);
  755. if (checkeof && ret >= 0) {
  756. int statret = ceph_do_getattr(inode,
  757. CEPH_STAT_CAP_SIZE);
  758. /* hit EOF or hole? */
  759. if (statret == 0 && iocb->ki_pos < inode->i_size &&
  760. ret < len) {
  761. dout("sync_read hit hole, ppos %lld < size %lld"
  762. ", reading more\n", iocb->ki_pos,
  763. inode->i_size);
  764. read += ret;
  765. len -= ret;
  766. checkeof = 0;
  767. goto again;
  768. }
  769. }
  770. if (ret >= 0)
  771. ret += read;
  772. return ret;
  773. }
  774. /*
  775. * Take cap references to avoid releasing caps to MDS mid-write.
  776. *
  777. * If we are synchronous, and write with an old snap context, the OSD
  778. * may return EOLDSNAPC. In that case, retry the write.. _after_
  779. * dropping our cap refs and allowing the pending snap to logically
  780. * complete _before_ this write occurs.
  781. *
  782. * If we are near ENOSPC, write synchronously.
  783. */
  784. static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
  785. unsigned long nr_segs, loff_t pos)
  786. {
  787. struct file *file = iocb->ki_filp;
  788. struct ceph_file_info *fi = file->private_data;
  789. struct inode *inode = file_inode(file);
  790. struct ceph_inode_info *ci = ceph_inode(inode);
  791. struct ceph_osd_client *osdc =
  792. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  793. ssize_t count, written = 0;
  794. int err, want, got;
  795. if (ceph_snap(inode) != CEPH_NOSNAP)
  796. return -EROFS;
  797. mutex_lock(&inode->i_mutex);
  798. err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
  799. if (err)
  800. goto out;
  801. /* We can write back this queue in page reclaim */
  802. current->backing_dev_info = file->f_mapping->backing_dev_info;
  803. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  804. if (err)
  805. goto out;
  806. if (count == 0)
  807. goto out;
  808. err = file_remove_suid(file);
  809. if (err)
  810. goto out;
  811. err = file_update_time(file);
  812. if (err)
  813. goto out;
  814. retry_snap:
  815. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) {
  816. err = -ENOSPC;
  817. goto out;
  818. }
  819. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  820. inode, ceph_vinop(inode), pos, count, inode->i_size);
  821. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  822. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  823. else
  824. want = CEPH_CAP_FILE_BUFFER;
  825. got = 0;
  826. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, pos + count);
  827. if (err < 0)
  828. goto out;
  829. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  830. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  831. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  832. (file->f_flags & O_DIRECT) || (fi->flags & CEPH_F_SYNC)) {
  833. mutex_unlock(&inode->i_mutex);
  834. if (file->f_flags & O_DIRECT)
  835. written = ceph_sync_direct_write(iocb, iov,
  836. nr_segs, count);
  837. else
  838. written = ceph_sync_write(iocb, iov, nr_segs, count);
  839. if (written == -EOLDSNAPC) {
  840. dout("aio_write %p %llx.%llx %llu~%u"
  841. "got EOLDSNAPC, retrying\n",
  842. inode, ceph_vinop(inode),
  843. pos, (unsigned)iov->iov_len);
  844. mutex_lock(&inode->i_mutex);
  845. goto retry_snap;
  846. }
  847. } else {
  848. /*
  849. * No need to acquire the i_truncate_mutex. Because
  850. * the MDS revokes Fwb caps before sending truncate
  851. * message to us. We can't get Fwb cap while there
  852. * are pending vmtruncate. So write and vmtruncate
  853. * can not run at the same time
  854. */
  855. written = generic_file_buffered_write(iocb, iov, nr_segs,
  856. pos, &iocb->ki_pos,
  857. count, 0);
  858. mutex_unlock(&inode->i_mutex);
  859. }
  860. if (written >= 0) {
  861. int dirty;
  862. spin_lock(&ci->i_ceph_lock);
  863. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  864. spin_unlock(&ci->i_ceph_lock);
  865. if (dirty)
  866. __mark_inode_dirty(inode, dirty);
  867. }
  868. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  869. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  870. ceph_cap_string(got));
  871. ceph_put_cap_refs(ci, got);
  872. if (written >= 0 &&
  873. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) ||
  874. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
  875. err = vfs_fsync_range(file, pos, pos + written - 1, 1);
  876. if (err < 0)
  877. written = err;
  878. }
  879. goto out_unlocked;
  880. out:
  881. mutex_unlock(&inode->i_mutex);
  882. out_unlocked:
  883. current->backing_dev_info = NULL;
  884. return written ? written : err;
  885. }
  886. /*
  887. * llseek. be sure to verify file size on SEEK_END.
  888. */
  889. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  890. {
  891. struct inode *inode = file->f_mapping->host;
  892. int ret;
  893. mutex_lock(&inode->i_mutex);
  894. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  895. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  896. if (ret < 0) {
  897. offset = ret;
  898. goto out;
  899. }
  900. }
  901. switch (whence) {
  902. case SEEK_END:
  903. offset += inode->i_size;
  904. break;
  905. case SEEK_CUR:
  906. /*
  907. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  908. * position-querying operation. Avoid rewriting the "same"
  909. * f_pos value back to the file because a concurrent read(),
  910. * write() or lseek() might have altered it
  911. */
  912. if (offset == 0) {
  913. offset = file->f_pos;
  914. goto out;
  915. }
  916. offset += file->f_pos;
  917. break;
  918. case SEEK_DATA:
  919. if (offset >= inode->i_size) {
  920. ret = -ENXIO;
  921. goto out;
  922. }
  923. break;
  924. case SEEK_HOLE:
  925. if (offset >= inode->i_size) {
  926. ret = -ENXIO;
  927. goto out;
  928. }
  929. offset = inode->i_size;
  930. break;
  931. }
  932. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  933. out:
  934. mutex_unlock(&inode->i_mutex);
  935. return offset;
  936. }
  937. static inline void ceph_zero_partial_page(
  938. struct inode *inode, loff_t offset, unsigned size)
  939. {
  940. struct page *page;
  941. pgoff_t index = offset >> PAGE_CACHE_SHIFT;
  942. page = find_lock_page(inode->i_mapping, index);
  943. if (page) {
  944. wait_on_page_writeback(page);
  945. zero_user(page, offset & (PAGE_CACHE_SIZE - 1), size);
  946. unlock_page(page);
  947. page_cache_release(page);
  948. }
  949. }
  950. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  951. loff_t length)
  952. {
  953. loff_t nearly = round_up(offset, PAGE_CACHE_SIZE);
  954. if (offset < nearly) {
  955. loff_t size = nearly - offset;
  956. if (length < size)
  957. size = length;
  958. ceph_zero_partial_page(inode, offset, size);
  959. offset += size;
  960. length -= size;
  961. }
  962. if (length >= PAGE_CACHE_SIZE) {
  963. loff_t size = round_down(length, PAGE_CACHE_SIZE);
  964. truncate_pagecache_range(inode, offset, offset + size - 1);
  965. offset += size;
  966. length -= size;
  967. }
  968. if (length)
  969. ceph_zero_partial_page(inode, offset, length);
  970. }
  971. static int ceph_zero_partial_object(struct inode *inode,
  972. loff_t offset, loff_t *length)
  973. {
  974. struct ceph_inode_info *ci = ceph_inode(inode);
  975. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  976. struct ceph_osd_request *req;
  977. int ret = 0;
  978. loff_t zero = 0;
  979. int op;
  980. if (!length) {
  981. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  982. length = &zero;
  983. } else {
  984. op = CEPH_OSD_OP_ZERO;
  985. }
  986. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  987. ceph_vino(inode),
  988. offset, length,
  989. 1, op,
  990. CEPH_OSD_FLAG_WRITE |
  991. CEPH_OSD_FLAG_ONDISK,
  992. NULL, 0, 0, false);
  993. if (IS_ERR(req)) {
  994. ret = PTR_ERR(req);
  995. goto out;
  996. }
  997. ceph_osdc_build_request(req, offset, NULL, ceph_vino(inode).snap,
  998. &inode->i_mtime);
  999. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1000. if (!ret) {
  1001. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1002. if (ret == -ENOENT)
  1003. ret = 0;
  1004. }
  1005. ceph_osdc_put_request(req);
  1006. out:
  1007. return ret;
  1008. }
  1009. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  1010. {
  1011. int ret = 0;
  1012. struct ceph_inode_info *ci = ceph_inode(inode);
  1013. s32 stripe_unit = ceph_file_layout_su(ci->i_layout);
  1014. s32 stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
  1015. s32 object_size = ceph_file_layout_object_size(ci->i_layout);
  1016. u64 object_set_size = object_size * stripe_count;
  1017. u64 nearly, t;
  1018. /* round offset up to next period boundary */
  1019. nearly = offset + object_set_size - 1;
  1020. t = nearly;
  1021. nearly -= do_div(t, object_set_size);
  1022. while (length && offset < nearly) {
  1023. loff_t size = length;
  1024. ret = ceph_zero_partial_object(inode, offset, &size);
  1025. if (ret < 0)
  1026. return ret;
  1027. offset += size;
  1028. length -= size;
  1029. }
  1030. while (length >= object_set_size) {
  1031. int i;
  1032. loff_t pos = offset;
  1033. for (i = 0; i < stripe_count; ++i) {
  1034. ret = ceph_zero_partial_object(inode, pos, NULL);
  1035. if (ret < 0)
  1036. return ret;
  1037. pos += stripe_unit;
  1038. }
  1039. offset += object_set_size;
  1040. length -= object_set_size;
  1041. }
  1042. while (length) {
  1043. loff_t size = length;
  1044. ret = ceph_zero_partial_object(inode, offset, &size);
  1045. if (ret < 0)
  1046. return ret;
  1047. offset += size;
  1048. length -= size;
  1049. }
  1050. return ret;
  1051. }
  1052. static long ceph_fallocate(struct file *file, int mode,
  1053. loff_t offset, loff_t length)
  1054. {
  1055. struct ceph_file_info *fi = file->private_data;
  1056. struct inode *inode = file_inode(file);
  1057. struct ceph_inode_info *ci = ceph_inode(inode);
  1058. struct ceph_osd_client *osdc =
  1059. &ceph_inode_to_client(inode)->client->osdc;
  1060. int want, got = 0;
  1061. int dirty;
  1062. int ret = 0;
  1063. loff_t endoff = 0;
  1064. loff_t size;
  1065. if (!S_ISREG(inode->i_mode))
  1066. return -EOPNOTSUPP;
  1067. if (IS_SWAPFILE(inode))
  1068. return -ETXTBSY;
  1069. mutex_lock(&inode->i_mutex);
  1070. if (ceph_snap(inode) != CEPH_NOSNAP) {
  1071. ret = -EROFS;
  1072. goto unlock;
  1073. }
  1074. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) &&
  1075. !(mode & FALLOC_FL_PUNCH_HOLE)) {
  1076. ret = -ENOSPC;
  1077. goto unlock;
  1078. }
  1079. size = i_size_read(inode);
  1080. if (!(mode & FALLOC_FL_KEEP_SIZE))
  1081. endoff = offset + length;
  1082. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1083. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1084. else
  1085. want = CEPH_CAP_FILE_BUFFER;
  1086. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, endoff);
  1087. if (ret < 0)
  1088. goto unlock;
  1089. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1090. if (offset < size)
  1091. ceph_zero_pagecache_range(inode, offset, length);
  1092. ret = ceph_zero_objects(inode, offset, length);
  1093. } else if (endoff > size) {
  1094. truncate_pagecache_range(inode, size, -1);
  1095. if (ceph_inode_set_size(inode, endoff))
  1096. ceph_check_caps(ceph_inode(inode),
  1097. CHECK_CAPS_AUTHONLY, NULL);
  1098. }
  1099. if (!ret) {
  1100. spin_lock(&ci->i_ceph_lock);
  1101. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  1102. spin_unlock(&ci->i_ceph_lock);
  1103. if (dirty)
  1104. __mark_inode_dirty(inode, dirty);
  1105. }
  1106. ceph_put_cap_refs(ci, got);
  1107. unlock:
  1108. mutex_unlock(&inode->i_mutex);
  1109. return ret;
  1110. }
  1111. const struct file_operations ceph_file_fops = {
  1112. .open = ceph_open,
  1113. .release = ceph_release,
  1114. .llseek = ceph_llseek,
  1115. .read = do_sync_read,
  1116. .write = do_sync_write,
  1117. .aio_read = ceph_aio_read,
  1118. .aio_write = ceph_aio_write,
  1119. .mmap = ceph_mmap,
  1120. .fsync = ceph_fsync,
  1121. .lock = ceph_lock,
  1122. .flock = ceph_flock,
  1123. .splice_read = generic_file_splice_read,
  1124. .splice_write = generic_file_splice_write,
  1125. .unlocked_ioctl = ceph_ioctl,
  1126. .compat_ioctl = ceph_ioctl,
  1127. .fallocate = ceph_fallocate,
  1128. };