ops_file.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. };
  473. static const u32 gfs2_to_iflags[32] = {
  474. [gfs2fl_Sync] = IFLAG_SYNC,
  475. [gfs2fl_Immutable] = IFLAG_IMMUTABLE,
  476. [gfs2fl_AppendOnly] = IFLAG_APPEND,
  477. [gfs2fl_NoAtime] = IFLAG_NOATIME,
  478. [gfs2fl_ExHash] = IFLAG_INDEX,
  479. [gfs2fl_Jdata] = IFLAG_JOURNAL_DATA,
  480. [gfs2fl_Directio] = IFLAG_DIRECTIO,
  481. [gfs2fl_InheritDirectio] = IFLAG_DIRECTIO,
  482. [gfs2fl_InheritJdata] = IFLAG_JOURNAL_DATA,
  483. };
  484. static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
  485. {
  486. struct inode *inode = filp->f_dentry->d_inode;
  487. struct gfs2_inode *ip = inode->u.generic_ip;
  488. struct gfs2_holder gh;
  489. int error;
  490. u32 iflags;
  491. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  492. error = gfs2_glock_nq_m_atime(1, &gh);
  493. if (error)
  494. return error;
  495. iflags = iflags_cvt(gfs2_to_iflags, ip->i_di.di_flags);
  496. if (put_user(iflags, ptr))
  497. error = -EFAULT;
  498. gfs2_glock_dq_m(1, &gh);
  499. gfs2_holder_uninit(&gh);
  500. return error;
  501. }
  502. /* Flags that can be set by user space */
  503. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  504. GFS2_DIF_DIRECTIO| \
  505. GFS2_DIF_IMMUTABLE| \
  506. GFS2_DIF_APPENDONLY| \
  507. GFS2_DIF_NOATIME| \
  508. GFS2_DIF_SYNC| \
  509. GFS2_DIF_SYSTEM| \
  510. GFS2_DIF_INHERIT_DIRECTIO| \
  511. GFS2_DIF_INHERIT_JDATA)
  512. /**
  513. * gfs2_set_flags - set flags on an inode
  514. * @inode: The inode
  515. * @flags: The flags to set
  516. * @mask: Indicates which flags are valid
  517. *
  518. */
  519. static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
  520. {
  521. struct inode *inode = filp->f_dentry->d_inode;
  522. struct gfs2_inode *ip = inode->u.generic_ip;
  523. struct gfs2_sbd *sdp = ip->i_sbd;
  524. struct buffer_head *bh;
  525. struct gfs2_holder gh;
  526. int error;
  527. u32 new_flags, flags;
  528. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  529. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  530. if (error) {
  531. gfs2_holder_uninit(&gh);
  532. return error;
  533. }
  534. flags = ip->i_di.di_flags;
  535. new_flags = (flags & ~mask) | (reqflags & mask);
  536. if ((new_flags ^ flags) == 0)
  537. goto out;
  538. if (S_ISDIR(inode->i_mode)) {
  539. if ((new_flags ^ flags) & GFS2_DIF_JDATA)
  540. new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
  541. if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
  542. new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
  543. }
  544. error = -EINVAL;
  545. if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
  546. goto out;
  547. error = -EPERM;
  548. if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
  549. goto out;
  550. if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
  551. goto out;
  552. if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
  553. !capable(CAP_LINUX_IMMUTABLE))
  554. goto out;
  555. if (!IS_IMMUTABLE(inode)) {
  556. error = gfs2_repermission(inode, MAY_WRITE, NULL);
  557. if (error)
  558. goto out;
  559. }
  560. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  561. if (error)
  562. goto out;
  563. error = gfs2_meta_inode_buffer(ip, &bh);
  564. if (error)
  565. goto out_trans_end;
  566. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  567. ip->i_di.di_flags = new_flags;
  568. gfs2_dinode_out(&ip->i_di, bh->b_data);
  569. brelse(bh);
  570. out_trans_end:
  571. gfs2_trans_end(sdp);
  572. out:
  573. gfs2_glock_dq_uninit(&gh);
  574. return error;
  575. }
  576. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  577. {
  578. u32 iflags, gfsflags;
  579. if (get_user(iflags, ptr))
  580. return -EFAULT;
  581. gfsflags = iflags_cvt(iflags_to_gfs2, iflags);
  582. return do_gfs2_set_flags(filp, gfsflags, ~0);
  583. }
  584. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  585. {
  586. switch(cmd) {
  587. case IFLAGS_GET_IOC:
  588. return gfs2_get_flags(filp, (u32 __user *)arg);
  589. case IFLAGS_SET_IOC:
  590. return gfs2_set_flags(filp, (u32 __user *)arg);
  591. }
  592. return -ENOTTY;
  593. }
  594. /**
  595. * gfs2_mmap -
  596. * @file: The file to map
  597. * @vma: The VMA which described the mapping
  598. *
  599. * Returns: 0 or error code
  600. */
  601. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  602. {
  603. struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
  604. struct gfs2_holder i_gh;
  605. int error;
  606. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  607. error = gfs2_glock_nq_atime(&i_gh);
  608. if (error) {
  609. gfs2_holder_uninit(&i_gh);
  610. return error;
  611. }
  612. /* This is VM_MAYWRITE instead of VM_WRITE because a call
  613. to mprotect() can turn on VM_WRITE later. */
  614. if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
  615. (VM_MAYSHARE | VM_MAYWRITE))
  616. vma->vm_ops = &gfs2_vm_ops_sharewrite;
  617. else
  618. vma->vm_ops = &gfs2_vm_ops_private;
  619. gfs2_glock_dq_uninit(&i_gh);
  620. return error;
  621. }
  622. /**
  623. * gfs2_open - open a file
  624. * @inode: the inode to open
  625. * @file: the struct file for this opening
  626. *
  627. * Returns: errno
  628. */
  629. static int gfs2_open(struct inode *inode, struct file *file)
  630. {
  631. struct gfs2_inode *ip = inode->u.generic_ip;
  632. struct gfs2_holder i_gh;
  633. struct gfs2_file *fp;
  634. int error;
  635. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  636. if (!fp)
  637. return -ENOMEM;
  638. mutex_init(&fp->f_fl_mutex);
  639. gfs2_assert_warn(ip->i_sbd, !file->private_data);
  640. file->private_data = fp;
  641. if (S_ISREG(ip->i_di.di_mode)) {
  642. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  643. &i_gh);
  644. if (error)
  645. goto fail;
  646. if (!(file->f_flags & O_LARGEFILE) &&
  647. ip->i_di.di_size > MAX_NON_LFS) {
  648. error = -EFBIG;
  649. goto fail_gunlock;
  650. }
  651. /* Listen to the Direct I/O flag */
  652. if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
  653. file->f_flags |= O_DIRECT;
  654. gfs2_glock_dq_uninit(&i_gh);
  655. }
  656. return 0;
  657. fail_gunlock:
  658. gfs2_glock_dq_uninit(&i_gh);
  659. fail:
  660. file->private_data = NULL;
  661. kfree(fp);
  662. return error;
  663. }
  664. /**
  665. * gfs2_close - called to close a struct file
  666. * @inode: the inode the struct file belongs to
  667. * @file: the struct file being closed
  668. *
  669. * Returns: errno
  670. */
  671. static int gfs2_close(struct inode *inode, struct file *file)
  672. {
  673. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  674. struct gfs2_file *fp;
  675. fp = file->private_data;
  676. file->private_data = NULL;
  677. if (gfs2_assert_warn(sdp, fp))
  678. return -EIO;
  679. kfree(fp);
  680. return 0;
  681. }
  682. /**
  683. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  684. * @file: the file that points to the dentry (we ignore this)
  685. * @dentry: the dentry that points to the inode to sync
  686. *
  687. * Returns: errno
  688. */
  689. static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
  690. {
  691. struct gfs2_inode *ip = dentry->d_inode->u.generic_ip;
  692. gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
  693. return 0;
  694. }
  695. /**
  696. * gfs2_lock - acquire/release a posix lock on a file
  697. * @file: the file pointer
  698. * @cmd: either modify or retrieve lock state, possibly wait
  699. * @fl: type and range of lock
  700. *
  701. * Returns: errno
  702. */
  703. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  704. {
  705. struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
  706. struct gfs2_sbd *sdp = ip->i_sbd;
  707. struct lm_lockname name =
  708. { .ln_number = ip->i_num.no_addr,
  709. .ln_type = LM_TYPE_PLOCK };
  710. if (!(fl->fl_flags & FL_POSIX))
  711. return -ENOLCK;
  712. if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  713. return -ENOLCK;
  714. if (sdp->sd_args.ar_localflocks) {
  715. if (IS_GETLK(cmd)) {
  716. struct file_lock tmp;
  717. int ret;
  718. ret = posix_test_lock(file, fl, &tmp);
  719. fl->fl_type = F_UNLCK;
  720. if (ret)
  721. memcpy(fl, &tmp, sizeof(struct file_lock));
  722. return 0;
  723. } else {
  724. return posix_lock_file_wait(file, fl);
  725. }
  726. }
  727. if (IS_GETLK(cmd))
  728. return gfs2_lm_plock_get(sdp, &name, file, fl);
  729. else if (fl->fl_type == F_UNLCK)
  730. return gfs2_lm_punlock(sdp, &name, file, fl);
  731. else
  732. return gfs2_lm_plock(sdp, &name, file, cmd, fl);
  733. }
  734. /**
  735. * gfs2_sendfile - Send bytes to a file or socket
  736. * @in_file: The file to read from
  737. * @out_file: The file to write to
  738. * @count: The amount of data
  739. * @offset: The beginning file offset
  740. *
  741. * Outputs: offset - updated according to number of bytes read
  742. *
  743. * Returns: The number of bytes sent, errno on failure
  744. */
  745. static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
  746. read_actor_t actor, void *target)
  747. {
  748. return generic_file_sendfile(in_file, offset, count, actor, target);
  749. }
  750. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  751. {
  752. struct gfs2_file *fp = file->private_data;
  753. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  754. struct gfs2_inode *ip = file->f_dentry->d_inode->u.generic_ip;
  755. struct gfs2_glock *gl;
  756. unsigned int state;
  757. int flags;
  758. int error = 0;
  759. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  760. flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
  761. mutex_lock(&fp->f_fl_mutex);
  762. gl = fl_gh->gh_gl;
  763. if (gl) {
  764. if (fl_gh->gh_state == state)
  765. goto out;
  766. gfs2_glock_hold(gl);
  767. flock_lock_file_wait(file,
  768. &(struct file_lock){.fl_type = F_UNLCK});
  769. gfs2_glock_dq_uninit(fl_gh);
  770. } else {
  771. error = gfs2_glock_get(ip->i_sbd,
  772. ip->i_num.no_addr, &gfs2_flock_glops,
  773. CREATE, &gl);
  774. if (error)
  775. goto out;
  776. }
  777. gfs2_holder_init(gl, state, flags, fl_gh);
  778. gfs2_glock_put(gl);
  779. error = gfs2_glock_nq(fl_gh);
  780. if (error) {
  781. gfs2_holder_uninit(fl_gh);
  782. if (error == GLR_TRYFAILED)
  783. error = -EAGAIN;
  784. } else {
  785. error = flock_lock_file_wait(file, fl);
  786. gfs2_assert_warn(ip->i_sbd, !error);
  787. }
  788. out:
  789. mutex_unlock(&fp->f_fl_mutex);
  790. return error;
  791. }
  792. static void do_unflock(struct file *file, struct file_lock *fl)
  793. {
  794. struct gfs2_file *fp = file->private_data;
  795. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  796. mutex_lock(&fp->f_fl_mutex);
  797. flock_lock_file_wait(file, fl);
  798. if (fl_gh->gh_gl)
  799. gfs2_glock_dq_uninit(fl_gh);
  800. mutex_unlock(&fp->f_fl_mutex);
  801. }
  802. /**
  803. * gfs2_flock - acquire/release a flock lock on a file
  804. * @file: the file pointer
  805. * @cmd: either modify or retrieve lock state, possibly wait
  806. * @fl: type and range of lock
  807. *
  808. * Returns: errno
  809. */
  810. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  811. {
  812. struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
  813. struct gfs2_sbd *sdp = ip->i_sbd;
  814. if (!(fl->fl_flags & FL_FLOCK))
  815. return -ENOLCK;
  816. if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  817. return -ENOLCK;
  818. if (sdp->sd_args.ar_localflocks)
  819. return flock_lock_file_wait(file, fl);
  820. if (fl->fl_type == F_UNLCK) {
  821. do_unflock(file, fl);
  822. return 0;
  823. } else
  824. return do_flock(file, cmd, fl);
  825. }
  826. struct file_operations gfs2_file_fops = {
  827. .llseek = gfs2_llseek,
  828. .read = gfs2_read,
  829. .readv = gfs2_file_readv,
  830. .aio_read = gfs2_file_aio_read,
  831. .write = generic_file_write,
  832. .writev = generic_file_writev,
  833. .aio_write = generic_file_aio_write,
  834. .unlocked_ioctl = gfs2_ioctl,
  835. .mmap = gfs2_mmap,
  836. .open = gfs2_open,
  837. .release = gfs2_close,
  838. .fsync = gfs2_fsync,
  839. .lock = gfs2_lock,
  840. .sendfile = gfs2_sendfile,
  841. .flock = gfs2_flock,
  842. .splice_read = generic_file_splice_read,
  843. .splice_write = generic_file_splice_write,
  844. };
  845. struct file_operations gfs2_dir_fops = {
  846. .readdir = gfs2_readdir,
  847. .unlocked_ioctl = gfs2_ioctl,
  848. .open = gfs2_open,
  849. .release = gfs2_close,
  850. .fsync = gfs2_fsync,
  851. .lock = gfs2_lock,
  852. .flock = gfs2_flock,
  853. };