ops_file.c 24 KB

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