ops_file.c 24 KB

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