ops_file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. /**
  133. * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
  134. * @opaque: opaque data used by the function
  135. * @name: the name of the directory entry
  136. * @length: the length of the name
  137. * @offset: the entry's offset in the directory
  138. * @inum: the inode number the entry points to
  139. * @type: the type of inode the entry points to
  140. *
  141. * Returns: 0 on success, 1 if buffer full
  142. */
  143. static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
  144. uint64_t offset, struct gfs2_inum *inum,
  145. unsigned int type)
  146. {
  147. struct filldir_reg *fdr = (struct filldir_reg *)opaque;
  148. struct gfs2_sbd *sdp = fdr->fdr_sbd;
  149. int error;
  150. error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
  151. inum->no_addr, type);
  152. if (error)
  153. return 1;
  154. if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
  155. gfs2_glock_prefetch_num(sdp,
  156. inum->no_addr, &gfs2_inode_glops,
  157. LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
  158. gfs2_glock_prefetch_num(sdp,
  159. inum->no_addr, &gfs2_iopen_glops,
  160. LM_ST_SHARED, LM_FLAG_TRY);
  161. }
  162. return 0;
  163. }
  164. /**
  165. * readdir_reg - Read directory entries from a directory
  166. * @file: The directory to read from
  167. * @dirent: Buffer for dirents
  168. * @filldir: Function used to do the copying
  169. *
  170. * Returns: errno
  171. */
  172. static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
  173. {
  174. struct inode *dir = file->f_mapping->host;
  175. struct gfs2_inode *dip = GFS2_I(dir);
  176. struct filldir_reg fdr;
  177. struct gfs2_holder d_gh;
  178. uint64_t offset = file->f_pos;
  179. int error;
  180. fdr.fdr_sbd = GFS2_SB(dir);
  181. fdr.fdr_prefetch = 1;
  182. fdr.fdr_filldir = filldir;
  183. fdr.fdr_opaque = dirent;
  184. gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
  185. error = gfs2_glock_nq_atime(&d_gh);
  186. if (error) {
  187. gfs2_holder_uninit(&d_gh);
  188. return error;
  189. }
  190. error = gfs2_dir_read(dir, &offset, &fdr, filldir_reg_func);
  191. gfs2_glock_dq_uninit(&d_gh);
  192. file->f_pos = offset;
  193. return error;
  194. }
  195. /**
  196. * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
  197. * @opaque: opaque data used by the function
  198. * @name: the name of the directory entry
  199. * @length: the length of the name
  200. * @offset: the entry's offset in the directory
  201. * @inum: the inode number the entry points to
  202. * @type: the type of inode the entry points to
  203. *
  204. * For supporting NFS.
  205. *
  206. * Returns: 0 on success, 1 if buffer full
  207. */
  208. static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
  209. uint64_t offset, struct gfs2_inum *inum,
  210. unsigned int type)
  211. {
  212. struct filldir_bad *fdb = (struct filldir_bad *)opaque;
  213. struct gfs2_sbd *sdp = fdb->fdb_sbd;
  214. struct filldir_bad_entry *fbe;
  215. if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
  216. fdb->fdb_name_off + length > fdb->fdb_name_size)
  217. return 1;
  218. fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
  219. fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
  220. memcpy(fbe->fbe_name, name, length);
  221. fbe->fbe_length = length;
  222. fbe->fbe_offset = offset;
  223. fbe->fbe_inum = *inum;
  224. fbe->fbe_type = type;
  225. fdb->fdb_entry_off++;
  226. fdb->fdb_name_off += length;
  227. if (!(length == 1 && *name == '.')) {
  228. gfs2_glock_prefetch_num(sdp,
  229. inum->no_addr, &gfs2_inode_glops,
  230. LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
  231. gfs2_glock_prefetch_num(sdp,
  232. inum->no_addr, &gfs2_iopen_glops,
  233. LM_ST_SHARED, LM_FLAG_TRY);
  234. }
  235. return 0;
  236. }
  237. /**
  238. * readdir_bad - Read directory entries from a directory
  239. * @file: The directory to read from
  240. * @dirent: Buffer for dirents
  241. * @filldir: Function used to do the copying
  242. *
  243. * For supporting NFS.
  244. *
  245. * Returns: errno
  246. */
  247. static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
  248. {
  249. struct inode *dir = file->f_mapping->host;
  250. struct gfs2_inode *dip = GFS2_I(dir);
  251. struct gfs2_sbd *sdp = GFS2_SB(dir);
  252. struct filldir_reg fdr;
  253. unsigned int entries, size;
  254. struct filldir_bad *fdb;
  255. struct gfs2_holder d_gh;
  256. uint64_t offset = file->f_pos;
  257. unsigned int x;
  258. struct filldir_bad_entry *fbe;
  259. int error;
  260. entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
  261. size = sizeof(struct filldir_bad) +
  262. entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
  263. fdb = kzalloc(size, GFP_KERNEL);
  264. if (!fdb)
  265. return -ENOMEM;
  266. fdb->fdb_sbd = sdp;
  267. fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
  268. fdb->fdb_entry_num = entries;
  269. fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
  270. entries * sizeof(struct filldir_bad_entry);
  271. fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
  272. gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
  273. error = gfs2_glock_nq_atime(&d_gh);
  274. if (error) {
  275. gfs2_holder_uninit(&d_gh);
  276. goto out;
  277. }
  278. error = gfs2_dir_read(dir, &offset, fdb, filldir_bad_func);
  279. gfs2_glock_dq_uninit(&d_gh);
  280. fdr.fdr_sbd = sdp;
  281. fdr.fdr_prefetch = 0;
  282. fdr.fdr_filldir = filldir;
  283. fdr.fdr_opaque = dirent;
  284. for (x = 0; x < fdb->fdb_entry_off; x++) {
  285. fbe = &fdb->fdb_entry[x];
  286. error = filldir_reg_func(&fdr,
  287. fbe->fbe_name, fbe->fbe_length,
  288. fbe->fbe_offset,
  289. &fbe->fbe_inum, fbe->fbe_type);
  290. if (error) {
  291. file->f_pos = fbe->fbe_offset;
  292. error = 0;
  293. goto out;
  294. }
  295. }
  296. file->f_pos = offset;
  297. out:
  298. kfree(fdb);
  299. return error;
  300. }
  301. /**
  302. * gfs2_readdir - Read directory entries from a directory
  303. * @file: The directory to read from
  304. * @dirent: Buffer for dirents
  305. * @filldir: Function used to do the copying
  306. *
  307. * Returns: errno
  308. */
  309. static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
  310. {
  311. int error;
  312. if (strcmp(current->comm, "nfsd") != 0)
  313. error = readdir_reg(file, dirent, filldir);
  314. else
  315. error = readdir_bad(file, dirent, filldir);
  316. return error;
  317. }
  318. static const u32 iflags_to_gfs2[32] = {
  319. [iflag_Sync] = GFS2_DIF_SYNC,
  320. [iflag_Immutable] = GFS2_DIF_IMMUTABLE,
  321. [iflag_Append] = GFS2_DIF_APPENDONLY,
  322. [iflag_NoAtime] = GFS2_DIF_NOATIME,
  323. [iflag_Index] = GFS2_DIF_EXHASH,
  324. [iflag_JournalData] = GFS2_DIF_JDATA,
  325. [iflag_DirectIO] = GFS2_DIF_DIRECTIO,
  326. };
  327. static const u32 gfs2_to_iflags[32] = {
  328. [gfs2fl_Sync] = IFLAG_SYNC,
  329. [gfs2fl_Immutable] = IFLAG_IMMUTABLE,
  330. [gfs2fl_AppendOnly] = IFLAG_APPEND,
  331. [gfs2fl_NoAtime] = IFLAG_NOATIME,
  332. [gfs2fl_ExHash] = IFLAG_INDEX,
  333. [gfs2fl_Jdata] = IFLAG_JOURNAL_DATA,
  334. [gfs2fl_Directio] = IFLAG_DIRECTIO,
  335. [gfs2fl_InheritDirectio] = IFLAG_DIRECTIO,
  336. [gfs2fl_InheritJdata] = IFLAG_JOURNAL_DATA,
  337. };
  338. static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
  339. {
  340. struct inode *inode = filp->f_dentry->d_inode;
  341. struct gfs2_inode *ip = GFS2_I(inode);
  342. struct gfs2_holder gh;
  343. int error;
  344. u32 iflags;
  345. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  346. error = gfs2_glock_nq_m_atime(1, &gh);
  347. if (error)
  348. return error;
  349. iflags = iflags_cvt(gfs2_to_iflags, ip->i_di.di_flags);
  350. if (put_user(iflags, ptr))
  351. error = -EFAULT;
  352. gfs2_glock_dq_m(1, &gh);
  353. gfs2_holder_uninit(&gh);
  354. return error;
  355. }
  356. /* Flags that can be set by user space */
  357. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  358. GFS2_DIF_DIRECTIO| \
  359. GFS2_DIF_IMMUTABLE| \
  360. GFS2_DIF_APPENDONLY| \
  361. GFS2_DIF_NOATIME| \
  362. GFS2_DIF_SYNC| \
  363. GFS2_DIF_SYSTEM| \
  364. GFS2_DIF_INHERIT_DIRECTIO| \
  365. GFS2_DIF_INHERIT_JDATA)
  366. /**
  367. * gfs2_set_flags - set flags on an inode
  368. * @inode: The inode
  369. * @flags: The flags to set
  370. * @mask: Indicates which flags are valid
  371. *
  372. */
  373. static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
  374. {
  375. struct inode *inode = filp->f_dentry->d_inode;
  376. struct gfs2_inode *ip = GFS2_I(inode);
  377. struct gfs2_sbd *sdp = GFS2_SB(inode);
  378. struct buffer_head *bh;
  379. struct gfs2_holder gh;
  380. int error;
  381. u32 new_flags, flags;
  382. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  383. if (error)
  384. return error;
  385. flags = ip->i_di.di_flags;
  386. new_flags = (flags & ~mask) | (reqflags & mask);
  387. if ((new_flags ^ flags) == 0)
  388. goto out;
  389. if (S_ISDIR(inode->i_mode)) {
  390. if ((new_flags ^ flags) & GFS2_DIF_JDATA)
  391. new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
  392. if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
  393. new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
  394. }
  395. error = -EINVAL;
  396. if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
  397. goto out;
  398. error = -EPERM;
  399. if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
  400. goto out;
  401. if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
  402. goto out;
  403. if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
  404. !capable(CAP_LINUX_IMMUTABLE))
  405. goto out;
  406. if (!IS_IMMUTABLE(inode)) {
  407. error = permission(inode, MAY_WRITE, NULL);
  408. if (error)
  409. goto out;
  410. }
  411. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  412. if (error)
  413. goto out;
  414. error = gfs2_meta_inode_buffer(ip, &bh);
  415. if (error)
  416. goto out_trans_end;
  417. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  418. ip->i_di.di_flags = new_flags;
  419. gfs2_dinode_out(&ip->i_di, bh->b_data);
  420. brelse(bh);
  421. out_trans_end:
  422. gfs2_trans_end(sdp);
  423. out:
  424. gfs2_glock_dq_uninit(&gh);
  425. return error;
  426. }
  427. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  428. {
  429. u32 iflags, gfsflags;
  430. if (get_user(iflags, ptr))
  431. return -EFAULT;
  432. gfsflags = iflags_cvt(iflags_to_gfs2, iflags);
  433. return do_gfs2_set_flags(filp, gfsflags, ~0);
  434. }
  435. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  436. {
  437. switch(cmd) {
  438. case IFLAGS_GET_IOC:
  439. return gfs2_get_flags(filp, (u32 __user *)arg);
  440. case IFLAGS_SET_IOC:
  441. return gfs2_set_flags(filp, (u32 __user *)arg);
  442. }
  443. return -ENOTTY;
  444. }
  445. /**
  446. * gfs2_mmap -
  447. * @file: The file to map
  448. * @vma: The VMA which described the mapping
  449. *
  450. * Returns: 0 or error code
  451. */
  452. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  453. {
  454. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  455. struct gfs2_holder i_gh;
  456. int error;
  457. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  458. error = gfs2_glock_nq_atime(&i_gh);
  459. if (error) {
  460. gfs2_holder_uninit(&i_gh);
  461. return error;
  462. }
  463. /* This is VM_MAYWRITE instead of VM_WRITE because a call
  464. to mprotect() can turn on VM_WRITE later. */
  465. if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
  466. (VM_MAYSHARE | VM_MAYWRITE))
  467. vma->vm_ops = &gfs2_vm_ops_sharewrite;
  468. else
  469. vma->vm_ops = &gfs2_vm_ops_private;
  470. gfs2_glock_dq_uninit(&i_gh);
  471. return error;
  472. }
  473. /**
  474. * gfs2_open - open a file
  475. * @inode: the inode to open
  476. * @file: the struct file for this opening
  477. *
  478. * Returns: errno
  479. */
  480. static int gfs2_open(struct inode *inode, struct file *file)
  481. {
  482. struct gfs2_inode *ip = GFS2_I(inode);
  483. struct gfs2_holder i_gh;
  484. struct gfs2_file *fp;
  485. int error;
  486. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  487. if (!fp)
  488. return -ENOMEM;
  489. mutex_init(&fp->f_fl_mutex);
  490. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  491. file->private_data = fp;
  492. if (S_ISREG(ip->i_di.di_mode)) {
  493. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  494. &i_gh);
  495. if (error)
  496. goto fail;
  497. if (!(file->f_flags & O_LARGEFILE) &&
  498. ip->i_di.di_size > MAX_NON_LFS) {
  499. error = -EFBIG;
  500. goto fail_gunlock;
  501. }
  502. /* Listen to the Direct I/O flag */
  503. if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
  504. file->f_flags |= O_DIRECT;
  505. gfs2_glock_dq_uninit(&i_gh);
  506. }
  507. return 0;
  508. fail_gunlock:
  509. gfs2_glock_dq_uninit(&i_gh);
  510. fail:
  511. file->private_data = NULL;
  512. kfree(fp);
  513. return error;
  514. }
  515. /**
  516. * gfs2_close - called to close a struct file
  517. * @inode: the inode the struct file belongs to
  518. * @file: the struct file being closed
  519. *
  520. * Returns: errno
  521. */
  522. static int gfs2_close(struct inode *inode, struct file *file)
  523. {
  524. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  525. struct gfs2_file *fp;
  526. fp = file->private_data;
  527. file->private_data = NULL;
  528. if (gfs2_assert_warn(sdp, fp))
  529. return -EIO;
  530. kfree(fp);
  531. return 0;
  532. }
  533. /**
  534. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  535. * @file: the file that points to the dentry (we ignore this)
  536. * @dentry: the dentry that points to the inode to sync
  537. *
  538. * Returns: errno
  539. */
  540. static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
  541. {
  542. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  543. gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
  544. return 0;
  545. }
  546. /**
  547. * gfs2_lock - acquire/release a posix lock on a file
  548. * @file: the file pointer
  549. * @cmd: either modify or retrieve lock state, possibly wait
  550. * @fl: type and range of lock
  551. *
  552. * Returns: errno
  553. */
  554. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  555. {
  556. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  557. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  558. struct lm_lockname name =
  559. { .ln_number = ip->i_num.no_addr,
  560. .ln_type = LM_TYPE_PLOCK };
  561. if (!(fl->fl_flags & FL_POSIX))
  562. return -ENOLCK;
  563. if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  564. return -ENOLCK;
  565. if (sdp->sd_args.ar_localflocks) {
  566. if (IS_GETLK(cmd)) {
  567. struct file_lock tmp;
  568. int ret;
  569. ret = posix_test_lock(file, fl, &tmp);
  570. fl->fl_type = F_UNLCK;
  571. if (ret)
  572. memcpy(fl, &tmp, sizeof(struct file_lock));
  573. return 0;
  574. } else {
  575. return posix_lock_file_wait(file, fl);
  576. }
  577. }
  578. if (IS_GETLK(cmd))
  579. return gfs2_lm_plock_get(sdp, &name, file, fl);
  580. else if (fl->fl_type == F_UNLCK)
  581. return gfs2_lm_punlock(sdp, &name, file, fl);
  582. else
  583. return gfs2_lm_plock(sdp, &name, file, cmd, fl);
  584. }
  585. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  586. {
  587. struct gfs2_file *fp = file->private_data;
  588. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  589. struct gfs2_inode *ip = GFS2_I(file->f_dentry->d_inode);
  590. struct gfs2_glock *gl;
  591. unsigned int state;
  592. int flags;
  593. int error = 0;
  594. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  595. flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
  596. mutex_lock(&fp->f_fl_mutex);
  597. gl = fl_gh->gh_gl;
  598. if (gl) {
  599. if (fl_gh->gh_state == state)
  600. goto out;
  601. gfs2_glock_hold(gl);
  602. flock_lock_file_wait(file,
  603. &(struct file_lock){.fl_type = F_UNLCK});
  604. gfs2_glock_dq_uninit(fl_gh);
  605. } else {
  606. error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
  607. ip->i_num.no_addr, &gfs2_flock_glops,
  608. CREATE, &gl);
  609. if (error)
  610. goto out;
  611. }
  612. gfs2_holder_init(gl, state, flags, fl_gh);
  613. gfs2_glock_put(gl);
  614. error = gfs2_glock_nq(fl_gh);
  615. if (error) {
  616. gfs2_holder_uninit(fl_gh);
  617. if (error == GLR_TRYFAILED)
  618. error = -EAGAIN;
  619. } else {
  620. error = flock_lock_file_wait(file, fl);
  621. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  622. }
  623. out:
  624. mutex_unlock(&fp->f_fl_mutex);
  625. return error;
  626. }
  627. static void do_unflock(struct file *file, struct file_lock *fl)
  628. {
  629. struct gfs2_file *fp = file->private_data;
  630. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  631. mutex_lock(&fp->f_fl_mutex);
  632. flock_lock_file_wait(file, fl);
  633. if (fl_gh->gh_gl)
  634. gfs2_glock_dq_uninit(fl_gh);
  635. mutex_unlock(&fp->f_fl_mutex);
  636. }
  637. /**
  638. * gfs2_flock - acquire/release a flock lock on a file
  639. * @file: the file pointer
  640. * @cmd: either modify or retrieve lock state, possibly wait
  641. * @fl: type and range of lock
  642. *
  643. * Returns: errno
  644. */
  645. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  646. {
  647. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  648. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  649. if (!(fl->fl_flags & FL_FLOCK))
  650. return -ENOLCK;
  651. if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  652. return -ENOLCK;
  653. if (sdp->sd_args.ar_localflocks)
  654. return flock_lock_file_wait(file, fl);
  655. if (fl->fl_type == F_UNLCK) {
  656. do_unflock(file, fl);
  657. return 0;
  658. } else
  659. return do_flock(file, cmd, fl);
  660. }
  661. const struct file_operations gfs2_file_fops = {
  662. .llseek = gfs2_llseek,
  663. .read = generic_file_read,
  664. .readv = generic_file_readv,
  665. .aio_read = generic_file_aio_read,
  666. .write = generic_file_write,
  667. .writev = generic_file_writev,
  668. .aio_write = generic_file_aio_write,
  669. .unlocked_ioctl = gfs2_ioctl,
  670. .mmap = gfs2_mmap,
  671. .open = gfs2_open,
  672. .release = gfs2_close,
  673. .fsync = gfs2_fsync,
  674. .lock = gfs2_lock,
  675. .sendfile = generic_file_sendfile,
  676. .flock = gfs2_flock,
  677. .splice_read = generic_file_splice_read,
  678. .splice_write = generic_file_splice_write,
  679. };
  680. const struct file_operations gfs2_dir_fops = {
  681. .readdir = gfs2_readdir,
  682. .unlocked_ioctl = gfs2_ioctl,
  683. .open = gfs2_open,
  684. .release = gfs2_close,
  685. .fsync = gfs2_fsync,
  686. .lock = gfs2_lock,
  687. .flock = gfs2_flock,
  688. };