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 iovec local_iov = { .iov_base = buf, .iov_len = count };
  268. BUG_ON(iocb->ki_pos != pos);
  269. return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
  270. }
  271. /**
  272. * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
  273. * @opaque: opaque data used by the function
  274. * @name: the name of the directory entry
  275. * @length: the length of the name
  276. * @offset: the entry's offset in the directory
  277. * @inum: the inode number the entry points to
  278. * @type: the type of inode the entry points to
  279. *
  280. * Returns: 0 on success, 1 if buffer full
  281. */
  282. static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
  283. uint64_t offset, struct gfs2_inum *inum,
  284. unsigned int type)
  285. {
  286. struct filldir_reg *fdr = (struct filldir_reg *)opaque;
  287. struct gfs2_sbd *sdp = fdr->fdr_sbd;
  288. int error;
  289. error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
  290. inum->no_formal_ino, type);
  291. if (error)
  292. return 1;
  293. if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
  294. gfs2_glock_prefetch_num(sdp,
  295. inum->no_addr, &gfs2_inode_glops,
  296. LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
  297. gfs2_glock_prefetch_num(sdp,
  298. inum->no_addr, &gfs2_iopen_glops,
  299. LM_ST_SHARED, LM_FLAG_TRY);
  300. }
  301. return 0;
  302. }
  303. /**
  304. * readdir_reg - Read directory entries from a directory
  305. * @file: The directory to read from
  306. * @dirent: Buffer for dirents
  307. * @filldir: Function used to do the copying
  308. *
  309. * Returns: errno
  310. */
  311. static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
  312. {
  313. struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
  314. struct filldir_reg fdr;
  315. struct gfs2_holder d_gh;
  316. uint64_t offset = file->f_pos;
  317. int error;
  318. fdr.fdr_sbd = dip->i_sbd;
  319. fdr.fdr_prefetch = 1;
  320. fdr.fdr_filldir = filldir;
  321. fdr.fdr_opaque = dirent;
  322. gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
  323. error = gfs2_glock_nq_atime(&d_gh);
  324. if (error) {
  325. gfs2_holder_uninit(&d_gh);
  326. return error;
  327. }
  328. error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
  329. gfs2_glock_dq_uninit(&d_gh);
  330. file->f_pos = offset;
  331. return error;
  332. }
  333. /**
  334. * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
  335. * @opaque: opaque data used by the function
  336. * @name: the name of the directory entry
  337. * @length: the length of the name
  338. * @offset: the entry's offset in the directory
  339. * @inum: the inode number the entry points to
  340. * @type: the type of inode the entry points to
  341. *
  342. * For supporting NFS.
  343. *
  344. * Returns: 0 on success, 1 if buffer full
  345. */
  346. static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
  347. uint64_t offset, struct gfs2_inum *inum,
  348. unsigned int type)
  349. {
  350. struct filldir_bad *fdb = (struct filldir_bad *)opaque;
  351. struct gfs2_sbd *sdp = fdb->fdb_sbd;
  352. struct filldir_bad_entry *fbe;
  353. if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
  354. fdb->fdb_name_off + length > fdb->fdb_name_size)
  355. return 1;
  356. fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
  357. fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
  358. memcpy(fbe->fbe_name, name, length);
  359. fbe->fbe_length = length;
  360. fbe->fbe_offset = offset;
  361. fbe->fbe_inum = *inum;
  362. fbe->fbe_type = type;
  363. fdb->fdb_entry_off++;
  364. fdb->fdb_name_off += length;
  365. if (!(length == 1 && *name == '.')) {
  366. gfs2_glock_prefetch_num(sdp,
  367. inum->no_addr, &gfs2_inode_glops,
  368. LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
  369. gfs2_glock_prefetch_num(sdp,
  370. inum->no_addr, &gfs2_iopen_glops,
  371. LM_ST_SHARED, LM_FLAG_TRY);
  372. }
  373. return 0;
  374. }
  375. /**
  376. * readdir_bad - Read directory entries from a directory
  377. * @file: The directory to read from
  378. * @dirent: Buffer for dirents
  379. * @filldir: Function used to do the copying
  380. *
  381. * For supporting NFS.
  382. *
  383. * Returns: errno
  384. */
  385. static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
  386. {
  387. struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
  388. struct gfs2_sbd *sdp = dip->i_sbd;
  389. struct filldir_reg fdr;
  390. unsigned int entries, size;
  391. struct filldir_bad *fdb;
  392. struct gfs2_holder d_gh;
  393. uint64_t offset = file->f_pos;
  394. unsigned int x;
  395. struct filldir_bad_entry *fbe;
  396. int error;
  397. entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
  398. size = sizeof(struct filldir_bad) +
  399. entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
  400. fdb = kzalloc(size, GFP_KERNEL);
  401. if (!fdb)
  402. return -ENOMEM;
  403. fdb->fdb_sbd = sdp;
  404. fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
  405. fdb->fdb_entry_num = entries;
  406. fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
  407. entries * sizeof(struct filldir_bad_entry);
  408. fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
  409. gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
  410. error = gfs2_glock_nq_atime(&d_gh);
  411. if (error) {
  412. gfs2_holder_uninit(&d_gh);
  413. goto out;
  414. }
  415. error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
  416. gfs2_glock_dq_uninit(&d_gh);
  417. fdr.fdr_sbd = sdp;
  418. fdr.fdr_prefetch = 0;
  419. fdr.fdr_filldir = filldir;
  420. fdr.fdr_opaque = dirent;
  421. for (x = 0; x < fdb->fdb_entry_off; x++) {
  422. fbe = &fdb->fdb_entry[x];
  423. error = filldir_reg_func(&fdr,
  424. fbe->fbe_name, fbe->fbe_length,
  425. fbe->fbe_offset,
  426. &fbe->fbe_inum, fbe->fbe_type);
  427. if (error) {
  428. file->f_pos = fbe->fbe_offset;
  429. error = 0;
  430. goto out;
  431. }
  432. }
  433. file->f_pos = offset;
  434. out:
  435. kfree(fdb);
  436. return error;
  437. }
  438. /**
  439. * gfs2_readdir - Read directory entries from a directory
  440. * @file: The directory to read from
  441. * @dirent: Buffer for dirents
  442. * @filldir: Function used to do the copying
  443. *
  444. * Returns: errno
  445. */
  446. static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
  447. {
  448. int error;
  449. if (strcmp(current->comm, "nfsd") != 0)
  450. error = readdir_reg(file, dirent, filldir);
  451. else
  452. error = readdir_bad(file, dirent, filldir);
  453. return error;
  454. }
  455. static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd,
  456. unsigned long arg)
  457. {
  458. unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ?
  459. 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 &
  485. (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
  486. if (!S_ISDIR(ip->i_di.di_mode))
  487. goto out;
  488. }
  489. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
  490. if (error)
  491. goto out;
  492. error = gfs2_meta_inode_buffer(ip, &dibh);
  493. if (error)
  494. goto out_trans_end;
  495. ip->i_di.di_flags = flags;
  496. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  497. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  498. brelse(dibh);
  499. out_trans_end:
  500. gfs2_trans_end(ip->i_sbd);
  501. } else {
  502. flags = ip->i_di.di_flags;
  503. }
  504. out:
  505. gfs2_glock_dq_uninit(&i_gh);
  506. if (cmd == GFS2_IOCTL_GETFLAGS) {
  507. if (put_user(flags, (__u32 __user *)arg))
  508. return -EFAULT;
  509. }
  510. return error;
  511. }
  512. /**
  513. * gfs2_ioctl - do an ioctl on a file
  514. * @inode: the inode
  515. * @file: the file pointer
  516. * @cmd: the ioctl command
  517. * @arg: the argument
  518. *
  519. * Returns: errno
  520. */
  521. static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  522. unsigned long arg)
  523. {
  524. struct gfs2_inode *ip = get_v2ip(inode);
  525. switch (cmd) {
  526. case GFS2_IOCTL_SETFLAGS:
  527. case GFS2_IOCTL_GETFLAGS:
  528. return gfs2_ioctl_flags(ip, cmd, arg);
  529. default:
  530. return -ENOTTY;
  531. }
  532. }
  533. /**
  534. * gfs2_mmap -
  535. * @file: The file to map
  536. * @vma: The VMA which described the mapping
  537. *
  538. * Returns: 0 or error code
  539. */
  540. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  541. {
  542. struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
  543. struct gfs2_holder i_gh;
  544. int error;
  545. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  546. error = gfs2_glock_nq_atime(&i_gh);
  547. if (error) {
  548. gfs2_holder_uninit(&i_gh);
  549. return error;
  550. }
  551. /* This is VM_MAYWRITE instead of VM_WRITE because a call
  552. to mprotect() can turn on VM_WRITE later. */
  553. if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
  554. (VM_MAYSHARE | VM_MAYWRITE))
  555. vma->vm_ops = &gfs2_vm_ops_sharewrite;
  556. else
  557. vma->vm_ops = &gfs2_vm_ops_private;
  558. gfs2_glock_dq_uninit(&i_gh);
  559. return error;
  560. }
  561. /**
  562. * gfs2_open - open a file
  563. * @inode: the inode to open
  564. * @file: the struct file for this opening
  565. *
  566. * Returns: errno
  567. */
  568. static int gfs2_open(struct inode *inode, struct file *file)
  569. {
  570. struct gfs2_inode *ip = get_v2ip(inode);
  571. struct gfs2_holder i_gh;
  572. struct gfs2_file *fp;
  573. int error;
  574. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  575. if (!fp)
  576. return -ENOMEM;
  577. mutex_init(&fp->f_fl_mutex);
  578. fp->f_inode = ip;
  579. fp->f_vfile = file;
  580. gfs2_assert_warn(ip->i_sbd, !get_v2fp(file));
  581. set_v2fp(file, fp);
  582. if (S_ISREG(ip->i_di.di_mode)) {
  583. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  584. &i_gh);
  585. if (error)
  586. goto fail;
  587. if (!(file->f_flags & O_LARGEFILE) &&
  588. ip->i_di.di_size > MAX_NON_LFS) {
  589. error = -EFBIG;
  590. goto fail_gunlock;
  591. }
  592. /* Listen to the Direct I/O flag */
  593. if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
  594. file->f_flags |= O_DIRECT;
  595. gfs2_glock_dq_uninit(&i_gh);
  596. }
  597. return 0;
  598. fail_gunlock:
  599. gfs2_glock_dq_uninit(&i_gh);
  600. fail:
  601. set_v2fp(file, NULL);
  602. kfree(fp);
  603. return error;
  604. }
  605. /**
  606. * gfs2_close - called to close a struct file
  607. * @inode: the inode the struct file belongs to
  608. * @file: the struct file being closed
  609. *
  610. * Returns: errno
  611. */
  612. static int gfs2_close(struct inode *inode, struct file *file)
  613. {
  614. struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb);
  615. struct gfs2_file *fp;
  616. fp = get_v2fp(file);
  617. set_v2fp(file, NULL);
  618. if (gfs2_assert_warn(sdp, fp))
  619. return -EIO;
  620. kfree(fp);
  621. return 0;
  622. }
  623. /**
  624. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  625. * @file: the file that points to the dentry (we ignore this)
  626. * @dentry: the dentry that points to the inode to sync
  627. *
  628. * Returns: errno
  629. */
  630. static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
  631. {
  632. struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
  633. gfs2_log_flush_glock(ip->i_gl);
  634. return 0;
  635. }
  636. /**
  637. * gfs2_lock - acquire/release a posix lock on a file
  638. * @file: the file pointer
  639. * @cmd: either modify or retrieve lock state, possibly wait
  640. * @fl: type and range of lock
  641. *
  642. * Returns: errno
  643. */
  644. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  645. {
  646. struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
  647. struct gfs2_sbd *sdp = ip->i_sbd;
  648. struct lm_lockname name =
  649. { .ln_number = ip->i_num.no_addr,
  650. .ln_type = LM_TYPE_PLOCK };
  651. if (!(fl->fl_flags & FL_POSIX))
  652. return -ENOLCK;
  653. if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  654. return -ENOLCK;
  655. if (sdp->sd_args.ar_localflocks) {
  656. if (IS_GETLK(cmd)) {
  657. struct file_lock *tmp;
  658. lock_kernel();
  659. tmp = posix_test_lock(file, fl);
  660. fl->fl_type = F_UNLCK;
  661. if (tmp)
  662. memcpy(fl, tmp, sizeof(struct file_lock));
  663. unlock_kernel();
  664. return 0;
  665. } else {
  666. int error;
  667. lock_kernel();
  668. error = posix_lock_file_wait(file, fl);
  669. unlock_kernel();
  670. return error;
  671. }
  672. }
  673. if (IS_GETLK(cmd))
  674. return gfs2_lm_plock_get(sdp, &name, file, fl);
  675. else if (fl->fl_type == F_UNLCK)
  676. return gfs2_lm_punlock(sdp, &name, file, fl);
  677. else
  678. return gfs2_lm_plock(sdp, &name, file, cmd, fl);
  679. }
  680. /**
  681. * gfs2_sendfile - Send bytes to a file or socket
  682. * @in_file: The file to read from
  683. * @out_file: The file to write to
  684. * @count: The amount of data
  685. * @offset: The beginning file offset
  686. *
  687. * Outputs: offset - updated according to number of bytes read
  688. *
  689. * Returns: The number of bytes sent, errno on failure
  690. */
  691. static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
  692. read_actor_t actor, void *target)
  693. {
  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. };