ops_file.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/uio.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/mm.h>
  18. #include <linux/smp_lock.h>
  19. #include <linux/gfs2_ioctl.h>
  20. #include <linux/fs.h>
  21. #include <asm/semaphore.h>
  22. #include <asm/uaccess.h>
  23. #include "gfs2.h"
  24. #include "bmap.h"
  25. #include "dir.h"
  26. #include "glock.h"
  27. #include "glops.h"
  28. #include "inode.h"
  29. #include "lm.h"
  30. #include "log.h"
  31. #include "meta_io.h"
  32. #include "ops_file.h"
  33. #include "ops_vm.h"
  34. #include "quota.h"
  35. #include "rgrp.h"
  36. #include "trans.h"
  37. /* "bad" is for NFS support */
  38. struct filldir_bad_entry {
  39. char *fbe_name;
  40. unsigned int fbe_length;
  41. uint64_t fbe_offset;
  42. struct gfs2_inum fbe_inum;
  43. unsigned int fbe_type;
  44. };
  45. struct filldir_bad {
  46. struct gfs2_sbd *fdb_sbd;
  47. struct filldir_bad_entry *fdb_entry;
  48. unsigned int fdb_entry_num;
  49. unsigned int fdb_entry_off;
  50. char *fdb_name;
  51. unsigned int fdb_name_size;
  52. unsigned int fdb_name_off;
  53. };
  54. /* For regular, non-NFS */
  55. struct filldir_reg {
  56. struct gfs2_sbd *fdr_sbd;
  57. int fdr_prefetch;
  58. filldir_t fdr_filldir;
  59. void *fdr_opaque;
  60. };
  61. /*
  62. * Most fields left uninitialised to catch anybody who tries to
  63. * use them. f_flags set to prevent file_accessed() from touching
  64. * any other part of this. Its use is purely as a flag so that we
  65. * know (in readpage()) whether or not do to locking.
  66. */
  67. struct file gfs2_internal_file_sentinal = {
  68. .f_flags = O_NOATIME|O_RDONLY,
  69. };
  70. static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
  71. unsigned long offset, unsigned long size)
  72. {
  73. char *kaddr;
  74. unsigned long count = desc->count;
  75. if (size > count)
  76. size = count;
  77. kaddr = kmap(page);
  78. memcpy(desc->arg.buf, kaddr + offset, size);
  79. kunmap(page);
  80. desc->count = count - size;
  81. desc->written += size;
  82. desc->arg.buf += size;
  83. return size;
  84. }
  85. int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
  86. char *buf, loff_t *pos, unsigned size)
  87. {
  88. struct inode *inode = ip->i_vnode;
  89. read_descriptor_t desc;
  90. desc.written = 0;
  91. desc.arg.buf = buf;
  92. desc.count = size;
  93. desc.error = 0;
  94. do_generic_mapping_read(inode->i_mapping, ra_state,
  95. &gfs2_internal_file_sentinal, pos, &desc,
  96. gfs2_read_actor);
  97. return desc.written ? desc.written : desc.error;
  98. }
  99. /**
  100. * gfs2_llseek - seek to a location in a file
  101. * @file: the file
  102. * @offset: the offset
  103. * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
  104. *
  105. * SEEK_END requires the glock for the file because it references the
  106. * file's size.
  107. *
  108. * Returns: The new offset, or errno
  109. */
  110. static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
  111. {
  112. struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
  113. struct gfs2_holder i_gh;
  114. loff_t error;
  115. if (origin == 2) {
  116. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  117. &i_gh);
  118. if (!error) {
  119. error = remote_llseek(file, offset, origin);
  120. gfs2_glock_dq_uninit(&i_gh);
  121. }
  122. } else
  123. error = remote_llseek(file, offset, origin);
  124. return error;
  125. }
  126. static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
  127. loff_t offset, unsigned long nr_segs)
  128. {
  129. struct file *file = iocb->ki_filp;
  130. struct address_space *mapping = file->f_mapping;
  131. ssize_t retval;
  132. retval = filemap_write_and_wait(mapping);
  133. if (retval == 0) {
  134. retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
  135. nr_segs);
  136. }
  137. return retval;
  138. }
  139. /**
  140. * __gfs2_file_aio_read - The main GFS2 read function
  141. *
  142. * N.B. This is almost, but not quite the same as __generic_file_aio_read()
  143. * the important subtle different being that inode->i_size isn't valid
  144. * unless we are holding a lock, and we do this _only_ on the O_DIRECT
  145. * path since otherwise locking is done entirely at the page cache
  146. * layer.
  147. */
  148. static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
  149. const struct iovec *iov,
  150. unsigned long nr_segs, loff_t *ppos)
  151. {
  152. struct file *filp = iocb->ki_filp;
  153. struct gfs2_inode *ip = get_v2ip(filp->f_mapping->host);
  154. struct gfs2_holder gh;
  155. ssize_t retval;
  156. unsigned long seg;
  157. size_t count;
  158. count = 0;
  159. for (seg = 0; seg < nr_segs; seg++) {
  160. const struct iovec *iv = &iov[seg];
  161. /*
  162. * If any segment has a negative length, or the cumulative
  163. * length ever wraps negative then return -EINVAL.
  164. */
  165. count += iv->iov_len;
  166. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  167. return -EINVAL;
  168. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  169. continue;
  170. if (seg == 0)
  171. return -EFAULT;
  172. nr_segs = seg;
  173. count -= iv->iov_len; /* This segment is no good */
  174. break;
  175. }
  176. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  177. if (filp->f_flags & O_DIRECT) {
  178. loff_t pos = *ppos, size;
  179. struct address_space *mapping;
  180. struct inode *inode;
  181. mapping = filp->f_mapping;
  182. inode = mapping->host;
  183. retval = 0;
  184. if (!count)
  185. goto out; /* skip atime */
  186. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  187. retval = gfs2_glock_nq_m_atime(1, &gh);
  188. if (retval)
  189. goto out;
  190. if (gfs2_is_stuffed(ip)) {
  191. gfs2_glock_dq_m(1, &gh);
  192. gfs2_holder_uninit(&gh);
  193. goto fallback_to_normal;
  194. }
  195. size = i_size_read(inode);
  196. if (pos < size) {
  197. retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
  198. if (retval > 0 && !is_sync_kiocb(iocb))
  199. retval = -EIOCBQUEUED;
  200. if (retval > 0)
  201. *ppos = pos + retval;
  202. }
  203. file_accessed(filp);
  204. gfs2_glock_dq_m(1, &gh);
  205. gfs2_holder_uninit(&gh);
  206. goto out;
  207. }
  208. fallback_to_normal:
  209. retval = 0;
  210. if (count) {
  211. for (seg = 0; seg < nr_segs; seg++) {
  212. read_descriptor_t desc;
  213. desc.written = 0;
  214. desc.arg.buf = iov[seg].iov_base;
  215. desc.count = iov[seg].iov_len;
  216. if (desc.count == 0)
  217. continue;
  218. desc.error = 0;
  219. do_generic_file_read(filp,ppos,&desc,file_read_actor);
  220. retval += desc.written;
  221. if (desc.error) {
  222. retval = retval ?: desc.error;
  223. break;
  224. }
  225. }
  226. }
  227. out:
  228. return retval;
  229. }
  230. /**
  231. * gfs2_read - Read bytes from a file
  232. * @file: The file to read from
  233. * @buf: The buffer to copy into
  234. * @size: The amount of data requested
  235. * @offset: The current file offset
  236. *
  237. * Outputs: Offset - updated according to number of bytes read
  238. *
  239. * Returns: The number of bytes read, errno on failure
  240. */
  241. static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
  242. loff_t *offset)
  243. {
  244. struct iovec local_iov = { .iov_base = buf, .iov_len = size };
  245. struct kiocb kiocb;
  246. ssize_t ret;
  247. init_sync_kiocb(&kiocb, filp);
  248. ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset);
  249. if (-EIOCBQUEUED == ret)
  250. ret = wait_on_sync_kiocb(&kiocb);
  251. return ret;
  252. }
  253. static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
  254. unsigned long nr_segs, loff_t *ppos)
  255. {
  256. struct kiocb kiocb;
  257. ssize_t ret;
  258. init_sync_kiocb(&kiocb, filp);
  259. ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos);
  260. if (-EIOCBQUEUED == ret)
  261. ret = wait_on_sync_kiocb(&kiocb);
  262. return ret;
  263. }
  264. static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
  265. size_t count, loff_t pos)
  266. {
  267. struct file *filp = iocb->ki_filp;
  268. struct iovec local_iov = { .iov_base = buf, .iov_len = count };
  269. BUG_ON(iocb->ki_pos != pos);
  270. return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
  271. }
  272. /**
  273. * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
  274. * @opaque: opaque data used by the function
  275. * @name: the name of the directory entry
  276. * @length: the length of the name
  277. * @offset: the entry's offset in the directory
  278. * @inum: the inode number the entry points to
  279. * @type: the type of inode the entry points to
  280. *
  281. * Returns: 0 on success, 1 if buffer full
  282. */
  283. static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
  284. uint64_t offset, struct gfs2_inum *inum,
  285. unsigned int type)
  286. {
  287. struct filldir_reg *fdr = (struct filldir_reg *)opaque;
  288. struct gfs2_sbd *sdp = fdr->fdr_sbd;
  289. int error;
  290. error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
  291. inum->no_formal_ino, type);
  292. if (error)
  293. return 1;
  294. if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
  295. gfs2_glock_prefetch_num(sdp,
  296. inum->no_addr, &gfs2_inode_glops,
  297. LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
  298. gfs2_glock_prefetch_num(sdp,
  299. inum->no_addr, &gfs2_iopen_glops,
  300. LM_ST_SHARED, LM_FLAG_TRY);
  301. }
  302. return 0;
  303. }
  304. /**
  305. * readdir_reg - Read directory entries from a directory
  306. * @file: The directory to read from
  307. * @dirent: Buffer for dirents
  308. * @filldir: Function used to do the copying
  309. *
  310. * Returns: errno
  311. */
  312. static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
  313. {
  314. struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
  315. struct filldir_reg fdr;
  316. struct gfs2_holder d_gh;
  317. uint64_t offset = file->f_pos;
  318. int error;
  319. fdr.fdr_sbd = dip->i_sbd;
  320. fdr.fdr_prefetch = 1;
  321. fdr.fdr_filldir = filldir;
  322. fdr.fdr_opaque = dirent;
  323. gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
  324. error = gfs2_glock_nq_atime(&d_gh);
  325. if (error) {
  326. gfs2_holder_uninit(&d_gh);
  327. return error;
  328. }
  329. error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
  330. gfs2_glock_dq_uninit(&d_gh);
  331. file->f_pos = offset;
  332. return error;
  333. }
  334. /**
  335. * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
  336. * @opaque: opaque data used by the function
  337. * @name: the name of the directory entry
  338. * @length: the length of the name
  339. * @offset: the entry's offset in the directory
  340. * @inum: the inode number the entry points to
  341. * @type: the type of inode the entry points to
  342. *
  343. * For supporting NFS.
  344. *
  345. * Returns: 0 on success, 1 if buffer full
  346. */
  347. static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
  348. uint64_t offset, struct gfs2_inum *inum,
  349. unsigned int type)
  350. {
  351. struct filldir_bad *fdb = (struct filldir_bad *)opaque;
  352. struct gfs2_sbd *sdp = fdb->fdb_sbd;
  353. struct filldir_bad_entry *fbe;
  354. if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
  355. fdb->fdb_name_off + length > fdb->fdb_name_size)
  356. return 1;
  357. fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
  358. fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
  359. memcpy(fbe->fbe_name, name, length);
  360. fbe->fbe_length = length;
  361. fbe->fbe_offset = offset;
  362. fbe->fbe_inum = *inum;
  363. fbe->fbe_type = type;
  364. fdb->fdb_entry_off++;
  365. fdb->fdb_name_off += length;
  366. if (!(length == 1 && *name == '.')) {
  367. gfs2_glock_prefetch_num(sdp,
  368. inum->no_addr, &gfs2_inode_glops,
  369. LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
  370. gfs2_glock_prefetch_num(sdp,
  371. inum->no_addr, &gfs2_iopen_glops,
  372. LM_ST_SHARED, LM_FLAG_TRY);
  373. }
  374. return 0;
  375. }
  376. /**
  377. * readdir_bad - Read directory entries from a directory
  378. * @file: The directory to read from
  379. * @dirent: Buffer for dirents
  380. * @filldir: Function used to do the copying
  381. *
  382. * For supporting NFS.
  383. *
  384. * Returns: errno
  385. */
  386. static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
  387. {
  388. struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
  389. struct gfs2_sbd *sdp = dip->i_sbd;
  390. struct filldir_reg fdr;
  391. unsigned int entries, size;
  392. struct filldir_bad *fdb;
  393. struct gfs2_holder d_gh;
  394. uint64_t offset = file->f_pos;
  395. unsigned int x;
  396. struct filldir_bad_entry *fbe;
  397. int error;
  398. entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
  399. size = sizeof(struct filldir_bad) +
  400. entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
  401. fdb = kzalloc(size, GFP_KERNEL);
  402. if (!fdb)
  403. return -ENOMEM;
  404. fdb->fdb_sbd = sdp;
  405. fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
  406. fdb->fdb_entry_num = entries;
  407. fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
  408. entries * sizeof(struct filldir_bad_entry);
  409. fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
  410. gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
  411. error = gfs2_glock_nq_atime(&d_gh);
  412. if (error) {
  413. gfs2_holder_uninit(&d_gh);
  414. goto out;
  415. }
  416. error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
  417. gfs2_glock_dq_uninit(&d_gh);
  418. fdr.fdr_sbd = sdp;
  419. fdr.fdr_prefetch = 0;
  420. fdr.fdr_filldir = filldir;
  421. fdr.fdr_opaque = dirent;
  422. for (x = 0; x < fdb->fdb_entry_off; x++) {
  423. fbe = &fdb->fdb_entry[x];
  424. error = filldir_reg_func(&fdr,
  425. fbe->fbe_name, fbe->fbe_length,
  426. fbe->fbe_offset,
  427. &fbe->fbe_inum, fbe->fbe_type);
  428. if (error) {
  429. file->f_pos = fbe->fbe_offset;
  430. error = 0;
  431. goto out;
  432. }
  433. }
  434. file->f_pos = offset;
  435. out:
  436. kfree(fdb);
  437. return error;
  438. }
  439. /**
  440. * gfs2_readdir - Read directory entries from a directory
  441. * @file: The directory to read from
  442. * @dirent: Buffer for dirents
  443. * @filldir: Function used to do the copying
  444. *
  445. * Returns: errno
  446. */
  447. static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
  448. {
  449. int error;
  450. if (strcmp(current->comm, "nfsd") != 0)
  451. error = readdir_reg(file, dirent, filldir);
  452. else
  453. error = readdir_bad(file, dirent, filldir);
  454. return error;
  455. }
  456. static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd,
  457. unsigned long arg)
  458. {
  459. unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  460. struct buffer_head *dibh;
  461. struct gfs2_holder i_gh;
  462. int error;
  463. __u32 flags = 0, change;
  464. if (cmd == GFS2_IOCTL_SETFLAGS) {
  465. error = get_user(flags, (__u32 __user *)arg);
  466. if (error)
  467. return -EFAULT;
  468. }
  469. error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh);
  470. if (error)
  471. return error;
  472. if (cmd == GFS2_IOCTL_SETFLAGS) {
  473. change = flags ^ ip->i_di.di_flags;
  474. error = -EPERM;
  475. if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) {
  476. if (!capable(CAP_LINUX_IMMUTABLE))
  477. goto out;
  478. }
  479. error = -EINVAL;
  480. if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) {
  481. if (!S_ISREG(ip->i_di.di_mode))
  482. goto out;
  483. }
  484. if (flags & (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
  485. if (!S_ISDIR(ip->i_di.di_mode))
  486. goto out;
  487. }
  488. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
  489. if (error)
  490. goto out;
  491. error = gfs2_meta_inode_buffer(ip, &dibh);
  492. if (error)
  493. goto out_trans_end;
  494. ip->i_di.di_flags = flags;
  495. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  496. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  497. brelse(dibh);
  498. out_trans_end:
  499. gfs2_trans_end(ip->i_sbd);
  500. } else {
  501. flags = ip->i_di.di_flags;
  502. }
  503. out:
  504. gfs2_glock_dq_uninit(&i_gh);
  505. if (cmd == GFS2_IOCTL_GETFLAGS) {
  506. if (put_user(flags, (__u32 __user *)arg))
  507. return -EFAULT;
  508. }
  509. return error;
  510. }
  511. /**
  512. * gfs2_ioctl - do an ioctl on a file
  513. * @inode: the inode
  514. * @file: the file pointer
  515. * @cmd: the ioctl command
  516. * @arg: the argument
  517. *
  518. * Returns: errno
  519. */
  520. static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  521. unsigned long arg)
  522. {
  523. struct gfs2_inode *ip = get_v2ip(inode);
  524. switch (cmd) {
  525. case GFS2_IOCTL_SETFLAGS:
  526. case GFS2_IOCTL_GETFLAGS:
  527. return gfs2_ioctl_flags(ip, cmd, arg);
  528. default:
  529. return -ENOTTY;
  530. }
  531. }
  532. /**
  533. * gfs2_mmap -
  534. * @file: The file to map
  535. * @vma: The VMA which described the mapping
  536. *
  537. * Returns: 0 or error code
  538. */
  539. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  540. {
  541. struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
  542. struct gfs2_holder i_gh;
  543. int error;
  544. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  545. error = gfs2_glock_nq_atime(&i_gh);
  546. if (error) {
  547. gfs2_holder_uninit(&i_gh);
  548. return error;
  549. }
  550. /* This is VM_MAYWRITE instead of VM_WRITE because a call
  551. to mprotect() can turn on VM_WRITE later. */
  552. if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
  553. (VM_MAYSHARE | VM_MAYWRITE))
  554. vma->vm_ops = &gfs2_vm_ops_sharewrite;
  555. else
  556. vma->vm_ops = &gfs2_vm_ops_private;
  557. gfs2_glock_dq_uninit(&i_gh);
  558. return error;
  559. }
  560. /**
  561. * gfs2_open - open a file
  562. * @inode: the inode to open
  563. * @file: the struct file for this opening
  564. *
  565. * Returns: errno
  566. */
  567. static int gfs2_open(struct inode *inode, struct file *file)
  568. {
  569. struct gfs2_inode *ip = get_v2ip(inode);
  570. struct gfs2_holder i_gh;
  571. struct gfs2_file *fp;
  572. int error;
  573. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  574. if (!fp)
  575. return -ENOMEM;
  576. mutex_init(&fp->f_fl_mutex);
  577. fp->f_inode = ip;
  578. fp->f_vfile = file;
  579. gfs2_assert_warn(ip->i_sbd, !get_v2fp(file));
  580. set_v2fp(file, fp);
  581. if (S_ISREG(ip->i_di.di_mode)) {
  582. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  583. &i_gh);
  584. if (error)
  585. goto fail;
  586. if (!(file->f_flags & O_LARGEFILE) &&
  587. ip->i_di.di_size > MAX_NON_LFS) {
  588. error = -EFBIG;
  589. goto fail_gunlock;
  590. }
  591. /* Listen to the Direct I/O flag */
  592. if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
  593. file->f_flags |= O_DIRECT;
  594. gfs2_glock_dq_uninit(&i_gh);
  595. }
  596. return 0;
  597. fail_gunlock:
  598. gfs2_glock_dq_uninit(&i_gh);
  599. fail:
  600. set_v2fp(file, NULL);
  601. kfree(fp);
  602. return error;
  603. }
  604. /**
  605. * gfs2_close - called to close a struct file
  606. * @inode: the inode the struct file belongs to
  607. * @file: the struct file being closed
  608. *
  609. * Returns: errno
  610. */
  611. static int gfs2_close(struct inode *inode, struct file *file)
  612. {
  613. struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb);
  614. struct gfs2_file *fp;
  615. fp = get_v2fp(file);
  616. set_v2fp(file, NULL);
  617. if (gfs2_assert_warn(sdp, fp))
  618. return -EIO;
  619. kfree(fp);
  620. return 0;
  621. }
  622. /**
  623. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  624. * @file: the file that points to the dentry (we ignore this)
  625. * @dentry: the dentry that points to the inode to sync
  626. *
  627. * Returns: errno
  628. */
  629. static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
  630. {
  631. struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
  632. gfs2_log_flush_glock(ip->i_gl);
  633. return 0;
  634. }
  635. /**
  636. * gfs2_lock - acquire/release a posix lock on a file
  637. * @file: the file pointer
  638. * @cmd: either modify or retrieve lock state, possibly wait
  639. * @fl: type and range of lock
  640. *
  641. * Returns: errno
  642. */
  643. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  644. {
  645. struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
  646. struct gfs2_sbd *sdp = ip->i_sbd;
  647. struct lm_lockname name =
  648. { .ln_number = ip->i_num.no_addr,
  649. .ln_type = LM_TYPE_PLOCK };
  650. if (!(fl->fl_flags & FL_POSIX))
  651. return -ENOLCK;
  652. if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  653. return -ENOLCK;
  654. if (sdp->sd_args.ar_localflocks) {
  655. if (IS_GETLK(cmd)) {
  656. struct file_lock *tmp;
  657. lock_kernel();
  658. tmp = posix_test_lock(file, fl);
  659. fl->fl_type = F_UNLCK;
  660. if (tmp)
  661. memcpy(fl, tmp, sizeof(struct file_lock));
  662. unlock_kernel();
  663. return 0;
  664. } else {
  665. int error;
  666. lock_kernel();
  667. error = posix_lock_file_wait(file, fl);
  668. unlock_kernel();
  669. return error;
  670. }
  671. }
  672. if (IS_GETLK(cmd))
  673. return gfs2_lm_plock_get(sdp, &name, file, fl);
  674. else if (fl->fl_type == F_UNLCK)
  675. return gfs2_lm_punlock(sdp, &name, file, fl);
  676. else
  677. return gfs2_lm_plock(sdp, &name, file, cmd, fl);
  678. }
  679. /**
  680. * gfs2_sendfile - Send bytes to a file or socket
  681. * @in_file: The file to read from
  682. * @out_file: The file to write to
  683. * @count: The amount of data
  684. * @offset: The beginning file offset
  685. *
  686. * Outputs: offset - updated according to number of bytes read
  687. *
  688. * Returns: The number of bytes sent, errno on failure
  689. */
  690. static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
  691. read_actor_t actor, void *target)
  692. {
  693. struct gfs2_inode *ip = get_v2ip(in_file->f_mapping->host);
  694. return generic_file_sendfile(in_file, offset, count, actor, target);
  695. }
  696. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  697. {
  698. struct gfs2_file *fp = get_v2fp(file);
  699. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  700. struct gfs2_inode *ip = fp->f_inode;
  701. struct gfs2_glock *gl;
  702. unsigned int state;
  703. int flags;
  704. int error = 0;
  705. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  706. flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
  707. mutex_lock(&fp->f_fl_mutex);
  708. gl = fl_gh->gh_gl;
  709. if (gl) {
  710. if (fl_gh->gh_state == state)
  711. goto out;
  712. gfs2_glock_hold(gl);
  713. flock_lock_file_wait(file,
  714. &(struct file_lock){.fl_type = F_UNLCK});
  715. gfs2_glock_dq_uninit(fl_gh);
  716. } else {
  717. error = gfs2_glock_get(ip->i_sbd,
  718. ip->i_num.no_addr, &gfs2_flock_glops,
  719. CREATE, &gl);
  720. if (error)
  721. goto out;
  722. }
  723. gfs2_holder_init(gl, state, flags, fl_gh);
  724. gfs2_glock_put(gl);
  725. error = gfs2_glock_nq(fl_gh);
  726. if (error) {
  727. gfs2_holder_uninit(fl_gh);
  728. if (error == GLR_TRYFAILED)
  729. error = -EAGAIN;
  730. } else {
  731. error = flock_lock_file_wait(file, fl);
  732. gfs2_assert_warn(ip->i_sbd, !error);
  733. }
  734. out:
  735. mutex_unlock(&fp->f_fl_mutex);
  736. return error;
  737. }
  738. static void do_unflock(struct file *file, struct file_lock *fl)
  739. {
  740. struct gfs2_file *fp = get_v2fp(file);
  741. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  742. mutex_lock(&fp->f_fl_mutex);
  743. flock_lock_file_wait(file, fl);
  744. if (fl_gh->gh_gl)
  745. gfs2_glock_dq_uninit(fl_gh);
  746. mutex_unlock(&fp->f_fl_mutex);
  747. }
  748. /**
  749. * gfs2_flock - acquire/release a flock lock on a file
  750. * @file: the file pointer
  751. * @cmd: either modify or retrieve lock state, possibly wait
  752. * @fl: type and range of lock
  753. *
  754. * Returns: errno
  755. */
  756. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  757. {
  758. struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
  759. struct gfs2_sbd *sdp = ip->i_sbd;
  760. if (!(fl->fl_flags & FL_FLOCK))
  761. return -ENOLCK;
  762. if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  763. return -ENOLCK;
  764. if (sdp->sd_args.ar_localflocks)
  765. return flock_lock_file_wait(file, fl);
  766. if (fl->fl_type == F_UNLCK) {
  767. do_unflock(file, fl);
  768. return 0;
  769. } else
  770. return do_flock(file, cmd, fl);
  771. }
  772. struct file_operations gfs2_file_fops = {
  773. .llseek = gfs2_llseek,
  774. .read = gfs2_read,
  775. .readv = gfs2_file_readv,
  776. .aio_read = gfs2_file_aio_read,
  777. .write = generic_file_write,
  778. .writev = generic_file_writev,
  779. .aio_write = generic_file_aio_write,
  780. .ioctl = gfs2_ioctl,
  781. .mmap = gfs2_mmap,
  782. .open = gfs2_open,
  783. .release = gfs2_close,
  784. .fsync = gfs2_fsync,
  785. .lock = gfs2_lock,
  786. .sendfile = gfs2_sendfile,
  787. .flock = gfs2_flock,
  788. };
  789. struct file_operations gfs2_dir_fops = {
  790. .readdir = gfs2_readdir,
  791. .ioctl = gfs2_ioctl,
  792. .open = gfs2_open,
  793. .release = gfs2_close,
  794. .fsync = gfs2_fsync,
  795. .lock = gfs2_lock,
  796. .flock = gfs2_flock,
  797. };