file.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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 file *file, char __user *data,
  366. unsigned len, loff_t *poff, int *checkeof)
  367. {
  368. struct inode *inode = file_inode(file);
  369. struct page **pages;
  370. u64 off = *poff;
  371. int num_pages, ret;
  372. dout("sync_read on file %p %llu~%u %s\n", file, off, len,
  373. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  374. if (file->f_flags & O_DIRECT) {
  375. num_pages = calc_pages_for((unsigned long)data, len);
  376. pages = ceph_get_direct_page_vector(data, num_pages, true);
  377. } else {
  378. num_pages = calc_pages_for(off, len);
  379. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  380. }
  381. if (IS_ERR(pages))
  382. return PTR_ERR(pages);
  383. /*
  384. * flush any page cache pages in this range. this
  385. * will make concurrent normal and sync io slow,
  386. * but it will at least behave sensibly when they are
  387. * in sequence.
  388. */
  389. ret = filemap_write_and_wait(inode->i_mapping);
  390. if (ret < 0)
  391. goto done;
  392. ret = striped_read(inode, off, len, pages, num_pages, checkeof,
  393. file->f_flags & O_DIRECT,
  394. (unsigned long)data & ~PAGE_MASK);
  395. if (ret >= 0 && (file->f_flags & O_DIRECT) == 0)
  396. ret = ceph_copy_page_vector_to_user(pages, data, off, ret);
  397. if (ret >= 0)
  398. *poff = off + ret;
  399. done:
  400. if (file->f_flags & O_DIRECT)
  401. ceph_put_page_vector(pages, num_pages, true);
  402. else
  403. ceph_release_page_vector(pages, num_pages);
  404. dout("sync_read result %d\n", ret);
  405. return ret;
  406. }
  407. /*
  408. * Write commit request unsafe callback, called to tell us when a
  409. * request is unsafe (that is, in flight--has been handed to the
  410. * messenger to send to its target osd). It is called again when
  411. * we've received a response message indicating the request is
  412. * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
  413. * is completed early (and unsuccessfully) due to a timeout or
  414. * interrupt.
  415. *
  416. * This is used if we requested both an ACK and ONDISK commit reply
  417. * from the OSD.
  418. */
  419. static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
  420. {
  421. struct ceph_inode_info *ci = ceph_inode(req->r_inode);
  422. dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
  423. unsafe ? "un" : "");
  424. if (unsafe) {
  425. ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
  426. spin_lock(&ci->i_unsafe_lock);
  427. list_add_tail(&req->r_unsafe_item,
  428. &ci->i_unsafe_writes);
  429. spin_unlock(&ci->i_unsafe_lock);
  430. } else {
  431. spin_lock(&ci->i_unsafe_lock);
  432. list_del_init(&req->r_unsafe_item);
  433. spin_unlock(&ci->i_unsafe_lock);
  434. ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
  435. }
  436. }
  437. /*
  438. * Synchronous write, straight from __user pointer or user pages (if
  439. * O_DIRECT).
  440. *
  441. * If write spans object boundary, just do multiple writes. (For a
  442. * correct atomic write, we should e.g. take write locks on all
  443. * objects, rollback on failure, etc.)
  444. */
  445. static ssize_t ceph_sync_write(struct file *file, const char __user *data,
  446. size_t left, loff_t pos, loff_t *ppos)
  447. {
  448. struct inode *inode = file_inode(file);
  449. struct ceph_inode_info *ci = ceph_inode(inode);
  450. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  451. struct ceph_snap_context *snapc;
  452. struct ceph_vino vino;
  453. struct ceph_osd_request *req;
  454. int num_ops = 1;
  455. struct page **pages;
  456. int num_pages;
  457. u64 len;
  458. int written = 0;
  459. int flags;
  460. int check_caps = 0;
  461. int page_align, io_align;
  462. unsigned long buf_align;
  463. int ret;
  464. struct timespec mtime = CURRENT_TIME;
  465. bool own_pages = false;
  466. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  467. return -EROFS;
  468. dout("sync_write on file %p %lld~%u %s\n", file, pos,
  469. (unsigned)left, (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  470. ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + left);
  471. if (ret < 0)
  472. return ret;
  473. ret = invalidate_inode_pages2_range(inode->i_mapping,
  474. pos >> PAGE_CACHE_SHIFT,
  475. (pos + left) >> PAGE_CACHE_SHIFT);
  476. if (ret < 0)
  477. dout("invalidate_inode_pages2_range returned %d\n", ret);
  478. flags = CEPH_OSD_FLAG_ORDERSNAP |
  479. CEPH_OSD_FLAG_ONDISK |
  480. CEPH_OSD_FLAG_WRITE;
  481. if ((file->f_flags & (O_SYNC|O_DIRECT)) == 0)
  482. flags |= CEPH_OSD_FLAG_ACK;
  483. else
  484. num_ops++; /* Also include a 'startsync' command. */
  485. /*
  486. * we may need to do multiple writes here if we span an object
  487. * boundary. this isn't atomic, unfortunately. :(
  488. */
  489. more:
  490. io_align = pos & ~PAGE_MASK;
  491. buf_align = (unsigned long)data & ~PAGE_MASK;
  492. len = left;
  493. snapc = ci->i_snap_realm->cached_context;
  494. vino = ceph_vino(inode);
  495. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  496. vino, pos, &len, num_ops,
  497. CEPH_OSD_OP_WRITE, flags, snapc,
  498. ci->i_truncate_seq, ci->i_truncate_size,
  499. false);
  500. if (IS_ERR(req))
  501. return PTR_ERR(req);
  502. /* write from beginning of first page, regardless of io alignment */
  503. page_align = file->f_flags & O_DIRECT ? buf_align : io_align;
  504. num_pages = calc_pages_for(page_align, len);
  505. if (file->f_flags & O_DIRECT) {
  506. pages = ceph_get_direct_page_vector(data, num_pages, false);
  507. if (IS_ERR(pages)) {
  508. ret = PTR_ERR(pages);
  509. goto out;
  510. }
  511. /*
  512. * throw out any page cache pages in this range. this
  513. * may block.
  514. */
  515. truncate_inode_pages_range(inode->i_mapping, pos,
  516. (pos+len) | (PAGE_CACHE_SIZE-1));
  517. } else {
  518. pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
  519. if (IS_ERR(pages)) {
  520. ret = PTR_ERR(pages);
  521. goto out;
  522. }
  523. ret = ceph_copy_user_to_page_vector(pages, data, pos, len);
  524. if (ret < 0) {
  525. ceph_release_page_vector(pages, num_pages);
  526. goto out;
  527. }
  528. if ((file->f_flags & O_SYNC) == 0) {
  529. /* get a second commit callback */
  530. req->r_unsafe_callback = ceph_sync_write_unsafe;
  531. req->r_inode = inode;
  532. own_pages = true;
  533. }
  534. }
  535. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
  536. false, own_pages);
  537. /* BUG_ON(vino.snap != CEPH_NOSNAP); */
  538. ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
  539. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  540. if (!ret)
  541. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  542. if (file->f_flags & O_DIRECT)
  543. ceph_put_page_vector(pages, num_pages, false);
  544. else if (file->f_flags & O_SYNC)
  545. ceph_release_page_vector(pages, num_pages);
  546. out:
  547. ceph_osdc_put_request(req);
  548. if (ret == 0) {
  549. pos += len;
  550. written += len;
  551. left -= len;
  552. data += len;
  553. if (left)
  554. goto more;
  555. ret = written;
  556. *ppos = pos;
  557. if (pos > i_size_read(inode))
  558. check_caps = ceph_inode_set_size(inode, pos);
  559. if (check_caps)
  560. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY,
  561. NULL);
  562. } else if (ret != -EOLDSNAPC && written > 0) {
  563. ret = written;
  564. }
  565. return ret;
  566. }
  567. /*
  568. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  569. * Atomically grab references, so that those bits are not released
  570. * back to the MDS mid-read.
  571. *
  572. * Hmm, the sync read case isn't actually async... should it be?
  573. */
  574. static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov,
  575. unsigned long nr_segs, loff_t pos)
  576. {
  577. struct file *filp = iocb->ki_filp;
  578. struct ceph_file_info *fi = filp->private_data;
  579. loff_t *ppos = &iocb->ki_pos;
  580. size_t len = iov->iov_len;
  581. struct inode *inode = file_inode(filp);
  582. struct ceph_inode_info *ci = ceph_inode(inode);
  583. void __user *base = iov->iov_base;
  584. ssize_t ret;
  585. int want, got = 0;
  586. int checkeof = 0, read = 0;
  587. dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
  588. inode, ceph_vinop(inode), pos, (unsigned)len, inode);
  589. again:
  590. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  591. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  592. else
  593. want = CEPH_CAP_FILE_CACHE;
  594. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, &got, -1);
  595. if (ret < 0)
  596. goto out;
  597. dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
  598. inode, ceph_vinop(inode), pos, (unsigned)len,
  599. ceph_cap_string(got));
  600. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  601. (iocb->ki_filp->f_flags & O_DIRECT) ||
  602. (fi->flags & CEPH_F_SYNC))
  603. /* hmm, this isn't really async... */
  604. ret = ceph_sync_read(filp, base, len, ppos, &checkeof);
  605. else
  606. ret = generic_file_aio_read(iocb, iov, nr_segs, pos);
  607. out:
  608. dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
  609. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  610. ceph_put_cap_refs(ci, got);
  611. if (checkeof && ret >= 0) {
  612. int statret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  613. /* hit EOF or hole? */
  614. if (statret == 0 && *ppos < inode->i_size) {
  615. dout("aio_read sync_read hit hole, ppos %lld < size %lld, reading more\n", *ppos, inode->i_size);
  616. read += ret;
  617. base += ret;
  618. len -= ret;
  619. checkeof = 0;
  620. goto again;
  621. }
  622. }
  623. if (ret >= 0)
  624. ret += read;
  625. return ret;
  626. }
  627. /*
  628. * Take cap references to avoid releasing caps to MDS mid-write.
  629. *
  630. * If we are synchronous, and write with an old snap context, the OSD
  631. * may return EOLDSNAPC. In that case, retry the write.. _after_
  632. * dropping our cap refs and allowing the pending snap to logically
  633. * complete _before_ this write occurs.
  634. *
  635. * If we are near ENOSPC, write synchronously.
  636. */
  637. static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
  638. unsigned long nr_segs, loff_t pos)
  639. {
  640. struct file *file = iocb->ki_filp;
  641. struct ceph_file_info *fi = file->private_data;
  642. struct inode *inode = file_inode(file);
  643. struct ceph_inode_info *ci = ceph_inode(inode);
  644. struct ceph_osd_client *osdc =
  645. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  646. ssize_t count, written = 0;
  647. int err, want, got;
  648. if (ceph_snap(inode) != CEPH_NOSNAP)
  649. return -EROFS;
  650. mutex_lock(&inode->i_mutex);
  651. err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
  652. if (err)
  653. goto out;
  654. /* We can write back this queue in page reclaim */
  655. current->backing_dev_info = file->f_mapping->backing_dev_info;
  656. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  657. if (err)
  658. goto out;
  659. if (count == 0)
  660. goto out;
  661. err = file_remove_suid(file);
  662. if (err)
  663. goto out;
  664. err = file_update_time(file);
  665. if (err)
  666. goto out;
  667. retry_snap:
  668. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) {
  669. err = -ENOSPC;
  670. goto out;
  671. }
  672. dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  673. inode, ceph_vinop(inode), pos, count, inode->i_size);
  674. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  675. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  676. else
  677. want = CEPH_CAP_FILE_BUFFER;
  678. got = 0;
  679. err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, pos + count);
  680. if (err < 0)
  681. goto out;
  682. dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
  683. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  684. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  685. (iocb->ki_filp->f_flags & O_DIRECT) ||
  686. (fi->flags & CEPH_F_SYNC)) {
  687. mutex_unlock(&inode->i_mutex);
  688. written = ceph_sync_write(file, iov->iov_base, count,
  689. pos, &iocb->ki_pos);
  690. if (written == -EOLDSNAPC) {
  691. dout("aio_write %p %llx.%llx %llu~%u"
  692. "got EOLDSNAPC, retrying\n",
  693. inode, ceph_vinop(inode),
  694. pos, (unsigned)iov->iov_len);
  695. mutex_lock(&inode->i_mutex);
  696. goto retry_snap;
  697. }
  698. } else {
  699. /*
  700. * No need to acquire the i_truncate_mutex. Because
  701. * the MDS revokes Fwb caps before sending truncate
  702. * message to us. We can't get Fwb cap while there
  703. * are pending vmtruncate. So write and vmtruncate
  704. * can not run at the same time
  705. */
  706. written = generic_file_buffered_write(iocb, iov, nr_segs,
  707. pos, &iocb->ki_pos,
  708. count, 0);
  709. mutex_unlock(&inode->i_mutex);
  710. }
  711. if (written >= 0) {
  712. int dirty;
  713. spin_lock(&ci->i_ceph_lock);
  714. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  715. spin_unlock(&ci->i_ceph_lock);
  716. if (dirty)
  717. __mark_inode_dirty(inode, dirty);
  718. }
  719. dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
  720. inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
  721. ceph_cap_string(got));
  722. ceph_put_cap_refs(ci, got);
  723. if (written >= 0 &&
  724. ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) ||
  725. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
  726. err = vfs_fsync_range(file, pos, pos + written - 1, 1);
  727. if (err < 0)
  728. written = err;
  729. }
  730. goto out_unlocked;
  731. out:
  732. mutex_unlock(&inode->i_mutex);
  733. out_unlocked:
  734. current->backing_dev_info = NULL;
  735. return written ? written : err;
  736. }
  737. /*
  738. * llseek. be sure to verify file size on SEEK_END.
  739. */
  740. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  741. {
  742. struct inode *inode = file->f_mapping->host;
  743. int ret;
  744. mutex_lock(&inode->i_mutex);
  745. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  746. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
  747. if (ret < 0) {
  748. offset = ret;
  749. goto out;
  750. }
  751. }
  752. switch (whence) {
  753. case SEEK_END:
  754. offset += inode->i_size;
  755. break;
  756. case SEEK_CUR:
  757. /*
  758. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  759. * position-querying operation. Avoid rewriting the "same"
  760. * f_pos value back to the file because a concurrent read(),
  761. * write() or lseek() might have altered it
  762. */
  763. if (offset == 0) {
  764. offset = file->f_pos;
  765. goto out;
  766. }
  767. offset += file->f_pos;
  768. break;
  769. case SEEK_DATA:
  770. if (offset >= inode->i_size) {
  771. ret = -ENXIO;
  772. goto out;
  773. }
  774. break;
  775. case SEEK_HOLE:
  776. if (offset >= inode->i_size) {
  777. ret = -ENXIO;
  778. goto out;
  779. }
  780. offset = inode->i_size;
  781. break;
  782. }
  783. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  784. out:
  785. mutex_unlock(&inode->i_mutex);
  786. return offset;
  787. }
  788. static inline void ceph_zero_partial_page(
  789. struct inode *inode, loff_t offset, unsigned size)
  790. {
  791. struct page *page;
  792. pgoff_t index = offset >> PAGE_CACHE_SHIFT;
  793. page = find_lock_page(inode->i_mapping, index);
  794. if (page) {
  795. wait_on_page_writeback(page);
  796. zero_user(page, offset & (PAGE_CACHE_SIZE - 1), size);
  797. unlock_page(page);
  798. page_cache_release(page);
  799. }
  800. }
  801. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  802. loff_t length)
  803. {
  804. loff_t nearly = round_up(offset, PAGE_CACHE_SIZE);
  805. if (offset < nearly) {
  806. loff_t size = nearly - offset;
  807. if (length < size)
  808. size = length;
  809. ceph_zero_partial_page(inode, offset, size);
  810. offset += size;
  811. length -= size;
  812. }
  813. if (length >= PAGE_CACHE_SIZE) {
  814. loff_t size = round_down(length, PAGE_CACHE_SIZE);
  815. truncate_pagecache_range(inode, offset, offset + size - 1);
  816. offset += size;
  817. length -= size;
  818. }
  819. if (length)
  820. ceph_zero_partial_page(inode, offset, length);
  821. }
  822. static int ceph_zero_partial_object(struct inode *inode,
  823. loff_t offset, loff_t *length)
  824. {
  825. struct ceph_inode_info *ci = ceph_inode(inode);
  826. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  827. struct ceph_osd_request *req;
  828. int ret = 0;
  829. loff_t zero = 0;
  830. int op;
  831. if (!length) {
  832. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  833. length = &zero;
  834. } else {
  835. op = CEPH_OSD_OP_ZERO;
  836. }
  837. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  838. ceph_vino(inode),
  839. offset, length,
  840. 1, op,
  841. CEPH_OSD_FLAG_WRITE |
  842. CEPH_OSD_FLAG_ONDISK,
  843. NULL, 0, 0, false);
  844. if (IS_ERR(req)) {
  845. ret = PTR_ERR(req);
  846. goto out;
  847. }
  848. ceph_osdc_build_request(req, offset, NULL, ceph_vino(inode).snap,
  849. &inode->i_mtime);
  850. ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  851. if (!ret) {
  852. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  853. if (ret == -ENOENT)
  854. ret = 0;
  855. }
  856. ceph_osdc_put_request(req);
  857. out:
  858. return ret;
  859. }
  860. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  861. {
  862. int ret = 0;
  863. struct ceph_inode_info *ci = ceph_inode(inode);
  864. s32 stripe_unit = ceph_file_layout_su(ci->i_layout);
  865. s32 stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
  866. s32 object_size = ceph_file_layout_object_size(ci->i_layout);
  867. u64 object_set_size = object_size * stripe_count;
  868. u64 nearly, t;
  869. /* round offset up to next period boundary */
  870. nearly = offset + object_set_size - 1;
  871. t = nearly;
  872. nearly -= do_div(t, object_set_size);
  873. while (length && offset < nearly) {
  874. loff_t size = length;
  875. ret = ceph_zero_partial_object(inode, offset, &size);
  876. if (ret < 0)
  877. return ret;
  878. offset += size;
  879. length -= size;
  880. }
  881. while (length >= object_set_size) {
  882. int i;
  883. loff_t pos = offset;
  884. for (i = 0; i < stripe_count; ++i) {
  885. ret = ceph_zero_partial_object(inode, pos, NULL);
  886. if (ret < 0)
  887. return ret;
  888. pos += stripe_unit;
  889. }
  890. offset += object_set_size;
  891. length -= object_set_size;
  892. }
  893. while (length) {
  894. loff_t size = length;
  895. ret = ceph_zero_partial_object(inode, offset, &size);
  896. if (ret < 0)
  897. return ret;
  898. offset += size;
  899. length -= size;
  900. }
  901. return ret;
  902. }
  903. static long ceph_fallocate(struct file *file, int mode,
  904. loff_t offset, loff_t length)
  905. {
  906. struct ceph_file_info *fi = file->private_data;
  907. struct inode *inode = file->f_dentry->d_inode;
  908. struct ceph_inode_info *ci = ceph_inode(inode);
  909. struct ceph_osd_client *osdc =
  910. &ceph_inode_to_client(inode)->client->osdc;
  911. int want, got = 0;
  912. int dirty;
  913. int ret = 0;
  914. loff_t endoff = 0;
  915. loff_t size;
  916. if (!S_ISREG(inode->i_mode))
  917. return -EOPNOTSUPP;
  918. if (IS_SWAPFILE(inode))
  919. return -ETXTBSY;
  920. mutex_lock(&inode->i_mutex);
  921. if (ceph_snap(inode) != CEPH_NOSNAP) {
  922. ret = -EROFS;
  923. goto unlock;
  924. }
  925. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) &&
  926. !(mode & FALLOC_FL_PUNCH_HOLE)) {
  927. ret = -ENOSPC;
  928. goto unlock;
  929. }
  930. size = i_size_read(inode);
  931. if (!(mode & FALLOC_FL_KEEP_SIZE))
  932. endoff = offset + length;
  933. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  934. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  935. else
  936. want = CEPH_CAP_FILE_BUFFER;
  937. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, endoff);
  938. if (ret < 0)
  939. goto unlock;
  940. if (mode & FALLOC_FL_PUNCH_HOLE) {
  941. if (offset < size)
  942. ceph_zero_pagecache_range(inode, offset, length);
  943. ret = ceph_zero_objects(inode, offset, length);
  944. } else if (endoff > size) {
  945. truncate_pagecache_range(inode, size, -1);
  946. if (ceph_inode_set_size(inode, endoff))
  947. ceph_check_caps(ceph_inode(inode),
  948. CHECK_CAPS_AUTHONLY, NULL);
  949. }
  950. if (!ret) {
  951. spin_lock(&ci->i_ceph_lock);
  952. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
  953. spin_unlock(&ci->i_ceph_lock);
  954. if (dirty)
  955. __mark_inode_dirty(inode, dirty);
  956. }
  957. ceph_put_cap_refs(ci, got);
  958. unlock:
  959. mutex_unlock(&inode->i_mutex);
  960. return ret;
  961. }
  962. const struct file_operations ceph_file_fops = {
  963. .open = ceph_open,
  964. .release = ceph_release,
  965. .llseek = ceph_llseek,
  966. .read = do_sync_read,
  967. .write = do_sync_write,
  968. .aio_read = ceph_aio_read,
  969. .aio_write = ceph_aio_write,
  970. .mmap = ceph_mmap,
  971. .fsync = ceph_fsync,
  972. .lock = ceph_lock,
  973. .flock = ceph_flock,
  974. .splice_read = generic_file_splice_read,
  975. .splice_write = generic_file_splice_write,
  976. .unlocked_ioctl = ceph_ioctl,
  977. .compat_ioctl = ceph_ioctl,
  978. .fallocate = ceph_fallocate,
  979. };