xfs_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_inum.h"
  22. #include "xfs_sb.h"
  23. #include "xfs_ag.h"
  24. #include "xfs_dir2.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_dmapi.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_alloc.h"
  32. #include "xfs_btree.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_error.h"
  38. #include "xfs_rw.h"
  39. #include "xfs_ioctl32.h"
  40. #include "xfs_vnodeops.h"
  41. #include <linux/dcache.h>
  42. #include <linux/smp_lock.h>
  43. static struct vm_operations_struct xfs_file_vm_ops;
  44. STATIC_INLINE ssize_t
  45. __xfs_file_read(
  46. struct kiocb *iocb,
  47. const struct iovec *iov,
  48. unsigned long nr_segs,
  49. int ioflags,
  50. loff_t pos)
  51. {
  52. struct file *file = iocb->ki_filp;
  53. BUG_ON(iocb->ki_pos != pos);
  54. if (unlikely(file->f_flags & O_DIRECT))
  55. ioflags |= IO_ISDIRECT;
  56. return xfs_read(XFS_I(file->f_path.dentry->d_inode), iocb, iov,
  57. nr_segs, &iocb->ki_pos, ioflags);
  58. }
  59. STATIC ssize_t
  60. xfs_file_aio_read(
  61. struct kiocb *iocb,
  62. const struct iovec *iov,
  63. unsigned long nr_segs,
  64. loff_t pos)
  65. {
  66. return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO, pos);
  67. }
  68. STATIC ssize_t
  69. xfs_file_aio_read_invis(
  70. struct kiocb *iocb,
  71. const struct iovec *iov,
  72. unsigned long nr_segs,
  73. loff_t pos)
  74. {
  75. return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
  76. }
  77. STATIC_INLINE ssize_t
  78. __xfs_file_write(
  79. struct kiocb *iocb,
  80. const struct iovec *iov,
  81. unsigned long nr_segs,
  82. int ioflags,
  83. loff_t pos)
  84. {
  85. struct file *file = iocb->ki_filp;
  86. BUG_ON(iocb->ki_pos != pos);
  87. if (unlikely(file->f_flags & O_DIRECT))
  88. ioflags |= IO_ISDIRECT;
  89. return xfs_write(XFS_I(file->f_mapping->host), iocb, iov, nr_segs,
  90. &iocb->ki_pos, ioflags);
  91. }
  92. STATIC ssize_t
  93. xfs_file_aio_write(
  94. struct kiocb *iocb,
  95. const struct iovec *iov,
  96. unsigned long nr_segs,
  97. loff_t pos)
  98. {
  99. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO, pos);
  100. }
  101. STATIC ssize_t
  102. xfs_file_aio_write_invis(
  103. struct kiocb *iocb,
  104. const struct iovec *iov,
  105. unsigned long nr_segs,
  106. loff_t pos)
  107. {
  108. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
  109. }
  110. STATIC ssize_t
  111. xfs_file_splice_read(
  112. struct file *infilp,
  113. loff_t *ppos,
  114. struct pipe_inode_info *pipe,
  115. size_t len,
  116. unsigned int flags)
  117. {
  118. return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
  119. infilp, ppos, pipe, len, flags, 0);
  120. }
  121. STATIC ssize_t
  122. xfs_file_splice_read_invis(
  123. struct file *infilp,
  124. loff_t *ppos,
  125. struct pipe_inode_info *pipe,
  126. size_t len,
  127. unsigned int flags)
  128. {
  129. return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
  130. infilp, ppos, pipe, len, flags, IO_INVIS);
  131. }
  132. STATIC ssize_t
  133. xfs_file_splice_write(
  134. struct pipe_inode_info *pipe,
  135. struct file *outfilp,
  136. loff_t *ppos,
  137. size_t len,
  138. unsigned int flags)
  139. {
  140. return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
  141. pipe, outfilp, ppos, len, flags, 0);
  142. }
  143. STATIC ssize_t
  144. xfs_file_splice_write_invis(
  145. struct pipe_inode_info *pipe,
  146. struct file *outfilp,
  147. loff_t *ppos,
  148. size_t len,
  149. unsigned int flags)
  150. {
  151. return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
  152. pipe, outfilp, ppos, len, flags, IO_INVIS);
  153. }
  154. STATIC int
  155. xfs_file_open(
  156. struct inode *inode,
  157. struct file *filp)
  158. {
  159. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  160. return -EFBIG;
  161. return -xfs_open(XFS_I(inode));
  162. }
  163. STATIC int
  164. xfs_file_release(
  165. struct inode *inode,
  166. struct file *filp)
  167. {
  168. return -xfs_release(XFS_I(inode));
  169. }
  170. /*
  171. * We ignore the datasync flag here because a datasync is effectively
  172. * identical to an fsync. That is, datasync implies that we need to write
  173. * only the metadata needed to be able to access the data that is written
  174. * if we crash after the call completes. Hence if we are writing beyond
  175. * EOF we have to log the inode size change as well, which makes it a
  176. * full fsync. If we don't write beyond EOF, the inode core will be
  177. * clean in memory and so we don't need to log the inode, just like
  178. * fsync.
  179. */
  180. STATIC int
  181. xfs_file_fsync(
  182. struct file *filp,
  183. struct dentry *dentry,
  184. int datasync)
  185. {
  186. xfs_iflags_clear(XFS_I(dentry->d_inode), XFS_ITRUNCATED);
  187. return -xfs_fsync(XFS_I(dentry->d_inode));
  188. }
  189. /*
  190. * Unfortunately we can't just use the clean and simple readdir implementation
  191. * below, because nfs might call back into ->lookup from the filldir callback
  192. * and that will deadlock the low-level btree code.
  193. *
  194. * Hopefully we'll find a better workaround that allows to use the optimal
  195. * version at least for local readdirs for 2.6.25.
  196. */
  197. #if 0
  198. STATIC int
  199. xfs_file_readdir(
  200. struct file *filp,
  201. void *dirent,
  202. filldir_t filldir)
  203. {
  204. struct inode *inode = filp->f_path.dentry->d_inode;
  205. xfs_inode_t *ip = XFS_I(inode);
  206. int error;
  207. size_t bufsize;
  208. /*
  209. * The Linux API doesn't pass down the total size of the buffer
  210. * we read into down to the filesystem. With the filldir concept
  211. * it's not needed for correct information, but the XFS dir2 leaf
  212. * code wants an estimate of the buffer size to calculate it's
  213. * readahead window and size the buffers used for mapping to
  214. * physical blocks.
  215. *
  216. * Try to give it an estimate that's good enough, maybe at some
  217. * point we can change the ->readdir prototype to include the
  218. * buffer size.
  219. */
  220. bufsize = (size_t)min_t(loff_t, PAGE_SIZE, inode->i_size);
  221. error = xfs_readdir(ip, dirent, bufsize,
  222. (xfs_off_t *)&filp->f_pos, filldir);
  223. if (error)
  224. return -error;
  225. return 0;
  226. }
  227. #else
  228. struct hack_dirent {
  229. u64 ino;
  230. loff_t offset;
  231. int namlen;
  232. unsigned int d_type;
  233. char name[];
  234. };
  235. struct hack_callback {
  236. char *dirent;
  237. size_t len;
  238. size_t used;
  239. };
  240. STATIC int
  241. xfs_hack_filldir(
  242. void *__buf,
  243. const char *name,
  244. int namlen,
  245. loff_t offset,
  246. u64 ino,
  247. unsigned int d_type)
  248. {
  249. struct hack_callback *buf = __buf;
  250. struct hack_dirent *de = (struct hack_dirent *)(buf->dirent + buf->used);
  251. unsigned int reclen;
  252. reclen = ALIGN(sizeof(struct hack_dirent) + namlen, sizeof(u64));
  253. if (buf->used + reclen > buf->len)
  254. return -EINVAL;
  255. de->namlen = namlen;
  256. de->offset = offset;
  257. de->ino = ino;
  258. de->d_type = d_type;
  259. memcpy(de->name, name, namlen);
  260. buf->used += reclen;
  261. return 0;
  262. }
  263. STATIC int
  264. xfs_file_readdir(
  265. struct file *filp,
  266. void *dirent,
  267. filldir_t filldir)
  268. {
  269. struct inode *inode = filp->f_path.dentry->d_inode;
  270. xfs_inode_t *ip = XFS_I(inode);
  271. struct hack_callback buf;
  272. struct hack_dirent *de;
  273. int error;
  274. loff_t size;
  275. int eof = 0;
  276. xfs_off_t start_offset, curr_offset, offset;
  277. /*
  278. * Try fairly hard to get memory
  279. */
  280. buf.len = PAGE_CACHE_SIZE;
  281. do {
  282. buf.dirent = kmalloc(buf.len, GFP_KERNEL);
  283. if (buf.dirent)
  284. break;
  285. buf.len >>= 1;
  286. } while (buf.len >= 1024);
  287. if (!buf.dirent)
  288. return -ENOMEM;
  289. curr_offset = filp->f_pos;
  290. if (curr_offset == 0x7fffffff)
  291. offset = 0xffffffff;
  292. else
  293. offset = filp->f_pos;
  294. while (!eof) {
  295. unsigned int reclen;
  296. start_offset = offset;
  297. buf.used = 0;
  298. error = -xfs_readdir(ip, &buf, buf.len, &offset,
  299. xfs_hack_filldir);
  300. if (error || offset == start_offset) {
  301. size = 0;
  302. break;
  303. }
  304. size = buf.used;
  305. de = (struct hack_dirent *)buf.dirent;
  306. while (size > 0) {
  307. curr_offset = de->offset /* & 0x7fffffff */;
  308. if (filldir(dirent, de->name, de->namlen,
  309. curr_offset & 0x7fffffff,
  310. de->ino, de->d_type)) {
  311. goto done;
  312. }
  313. reclen = ALIGN(sizeof(struct hack_dirent) + de->namlen,
  314. sizeof(u64));
  315. size -= reclen;
  316. de = (struct hack_dirent *)((char *)de + reclen);
  317. }
  318. }
  319. done:
  320. if (!error) {
  321. if (size == 0)
  322. filp->f_pos = offset & 0x7fffffff;
  323. else if (de)
  324. filp->f_pos = curr_offset;
  325. }
  326. kfree(buf.dirent);
  327. return error;
  328. }
  329. #endif
  330. STATIC int
  331. xfs_file_mmap(
  332. struct file *filp,
  333. struct vm_area_struct *vma)
  334. {
  335. vma->vm_ops = &xfs_file_vm_ops;
  336. vma->vm_flags |= VM_CAN_NONLINEAR;
  337. file_accessed(filp);
  338. return 0;
  339. }
  340. STATIC long
  341. xfs_file_ioctl(
  342. struct file *filp,
  343. unsigned int cmd,
  344. unsigned long p)
  345. {
  346. int error;
  347. struct inode *inode = filp->f_path.dentry->d_inode;
  348. error = xfs_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
  349. xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
  350. /* NOTE: some of the ioctl's return positive #'s as a
  351. * byte count indicating success, such as
  352. * readlink_by_handle. So we don't "sign flip"
  353. * like most other routines. This means true
  354. * errors need to be returned as a negative value.
  355. */
  356. return error;
  357. }
  358. STATIC long
  359. xfs_file_ioctl_invis(
  360. struct file *filp,
  361. unsigned int cmd,
  362. unsigned long p)
  363. {
  364. int error;
  365. struct inode *inode = filp->f_path.dentry->d_inode;
  366. error = xfs_ioctl(XFS_I(inode), filp, IO_INVIS, cmd, (void __user *)p);
  367. xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
  368. /* NOTE: some of the ioctl's return positive #'s as a
  369. * byte count indicating success, such as
  370. * readlink_by_handle. So we don't "sign flip"
  371. * like most other routines. This means true
  372. * errors need to be returned as a negative value.
  373. */
  374. return error;
  375. }
  376. /*
  377. * mmap()d file has taken write protection fault and is being made
  378. * writable. We can set the page state up correctly for a writable
  379. * page, which means we can do correct delalloc accounting (ENOSPC
  380. * checking!) and unwritten extent mapping.
  381. */
  382. STATIC int
  383. xfs_vm_page_mkwrite(
  384. struct vm_area_struct *vma,
  385. struct page *page)
  386. {
  387. return block_page_mkwrite(vma, page, xfs_get_blocks);
  388. }
  389. const struct file_operations xfs_file_operations = {
  390. .llseek = generic_file_llseek,
  391. .read = do_sync_read,
  392. .write = do_sync_write,
  393. .aio_read = xfs_file_aio_read,
  394. .aio_write = xfs_file_aio_write,
  395. .splice_read = xfs_file_splice_read,
  396. .splice_write = xfs_file_splice_write,
  397. .unlocked_ioctl = xfs_file_ioctl,
  398. #ifdef CONFIG_COMPAT
  399. .compat_ioctl = xfs_file_compat_ioctl,
  400. #endif
  401. .mmap = xfs_file_mmap,
  402. .open = xfs_file_open,
  403. .release = xfs_file_release,
  404. .fsync = xfs_file_fsync,
  405. #ifdef HAVE_FOP_OPEN_EXEC
  406. .open_exec = xfs_file_open_exec,
  407. #endif
  408. };
  409. const struct file_operations xfs_invis_file_operations = {
  410. .llseek = generic_file_llseek,
  411. .read = do_sync_read,
  412. .write = do_sync_write,
  413. .aio_read = xfs_file_aio_read_invis,
  414. .aio_write = xfs_file_aio_write_invis,
  415. .splice_read = xfs_file_splice_read_invis,
  416. .splice_write = xfs_file_splice_write_invis,
  417. .unlocked_ioctl = xfs_file_ioctl_invis,
  418. #ifdef CONFIG_COMPAT
  419. .compat_ioctl = xfs_file_compat_invis_ioctl,
  420. #endif
  421. .mmap = xfs_file_mmap,
  422. .open = xfs_file_open,
  423. .release = xfs_file_release,
  424. .fsync = xfs_file_fsync,
  425. };
  426. const struct file_operations xfs_dir_file_operations = {
  427. .read = generic_read_dir,
  428. .readdir = xfs_file_readdir,
  429. .llseek = generic_file_llseek,
  430. .unlocked_ioctl = xfs_file_ioctl,
  431. #ifdef CONFIG_COMPAT
  432. .compat_ioctl = xfs_file_compat_ioctl,
  433. #endif
  434. .fsync = xfs_file_fsync,
  435. };
  436. static struct vm_operations_struct xfs_file_vm_ops = {
  437. .fault = filemap_fault,
  438. .page_mkwrite = xfs_vm_page_mkwrite,
  439. };