file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * file.c
  3. *
  4. * Copyright (C) 1995, 1996, 1997 by Paal-Kr. Engstad and Volker Lendecke
  5. * Copyright (C) 1997 by Volker Lendecke
  6. *
  7. * Please add a note about your changes to smbfs in the ChangeLog file.
  8. */
  9. #include <linux/time.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/fcntl.h>
  13. #include <linux/stat.h>
  14. #include <linux/mm.h>
  15. #include <linux/slab.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/smp_lock.h>
  18. #include <linux/net.h>
  19. #include <linux/aio.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/system.h>
  22. #include <linux/smbno.h>
  23. #include <linux/smb_fs.h>
  24. #include "smb_debug.h"
  25. #include "proto.h"
  26. static int
  27. smb_fsync(struct file *file, struct dentry * dentry, int datasync)
  28. {
  29. struct smb_sb_info *server = server_from_dentry(dentry);
  30. int result;
  31. VERBOSE("sync file %s/%s\n", DENTRY_PATH(dentry));
  32. /*
  33. * The VFS will writepage() all dirty pages for us, but we
  34. * should send a SMBflush to the server, letting it know that
  35. * we want things synchronized with actual storage.
  36. *
  37. * Note: this function requires all pages to have been written already
  38. * (should be ok with writepage_sync)
  39. */
  40. result = smb_proc_flush(server, SMB_I(dentry->d_inode)->fileid);
  41. return result;
  42. }
  43. /*
  44. * Read a page synchronously.
  45. */
  46. static int
  47. smb_readpage_sync(struct dentry *dentry, struct page *page)
  48. {
  49. char *buffer = kmap(page);
  50. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  51. struct smb_sb_info *server = server_from_dentry(dentry);
  52. unsigned int rsize = smb_get_rsize(server);
  53. int count = PAGE_SIZE;
  54. int result;
  55. VERBOSE("file %s/%s, count=%d@%Ld, rsize=%d\n",
  56. DENTRY_PATH(dentry), count, offset, rsize);
  57. result = smb_open(dentry, SMB_O_RDONLY);
  58. if (result < 0)
  59. goto io_error;
  60. do {
  61. if (count < rsize)
  62. rsize = count;
  63. result = server->ops->read(dentry->d_inode,offset,rsize,buffer);
  64. if (result < 0)
  65. goto io_error;
  66. count -= result;
  67. offset += result;
  68. buffer += result;
  69. dentry->d_inode->i_atime =
  70. current_fs_time(dentry->d_inode->i_sb);
  71. if (result < rsize)
  72. break;
  73. } while (count);
  74. memset(buffer, 0, count);
  75. flush_dcache_page(page);
  76. SetPageUptodate(page);
  77. result = 0;
  78. io_error:
  79. kunmap(page);
  80. unlock_page(page);
  81. return result;
  82. }
  83. /*
  84. * We are called with the page locked and we unlock it when done.
  85. */
  86. static int
  87. smb_readpage(struct file *file, struct page *page)
  88. {
  89. int error;
  90. struct dentry *dentry = file->f_path.dentry;
  91. page_cache_get(page);
  92. error = smb_readpage_sync(dentry, page);
  93. page_cache_release(page);
  94. return error;
  95. }
  96. /*
  97. * Write a page synchronously.
  98. * Offset is the data offset within the page.
  99. */
  100. static int
  101. smb_writepage_sync(struct inode *inode, struct page *page,
  102. unsigned long pageoffset, unsigned int count)
  103. {
  104. loff_t offset;
  105. char *buffer = kmap(page) + pageoffset;
  106. struct smb_sb_info *server = server_from_inode(inode);
  107. unsigned int wsize = smb_get_wsize(server);
  108. int ret = 0;
  109. offset = ((loff_t)page->index << PAGE_CACHE_SHIFT) + pageoffset;
  110. VERBOSE("file ino=%ld, fileid=%d, count=%d@%Ld, wsize=%d\n",
  111. inode->i_ino, SMB_I(inode)->fileid, count, offset, wsize);
  112. do {
  113. int write_ret;
  114. if (count < wsize)
  115. wsize = count;
  116. write_ret = server->ops->write(inode, offset, wsize, buffer);
  117. if (write_ret < 0) {
  118. PARANOIA("failed write, wsize=%d, write_ret=%d\n",
  119. wsize, write_ret);
  120. ret = write_ret;
  121. break;
  122. }
  123. /* N.B. what if result < wsize?? */
  124. #ifdef SMBFS_PARANOIA
  125. if (write_ret < wsize)
  126. PARANOIA("short write, wsize=%d, write_ret=%d\n",
  127. wsize, write_ret);
  128. #endif
  129. buffer += wsize;
  130. offset += wsize;
  131. count -= wsize;
  132. /*
  133. * Update the inode now rather than waiting for a refresh.
  134. */
  135. inode->i_mtime = inode->i_atime = current_fs_time(inode->i_sb);
  136. SMB_I(inode)->flags |= SMB_F_LOCALWRITE;
  137. if (offset > inode->i_size)
  138. inode->i_size = offset;
  139. } while (count);
  140. kunmap(page);
  141. return ret;
  142. }
  143. /*
  144. * Write a page to the server. This will be used for NFS swapping only
  145. * (for now), and we currently do this synchronously only.
  146. *
  147. * We are called with the page locked and we unlock it when done.
  148. */
  149. static int
  150. smb_writepage(struct page *page, struct writeback_control *wbc)
  151. {
  152. struct address_space *mapping = page->mapping;
  153. struct inode *inode;
  154. unsigned long end_index;
  155. unsigned offset = PAGE_CACHE_SIZE;
  156. int err;
  157. BUG_ON(!mapping);
  158. inode = mapping->host;
  159. BUG_ON(!inode);
  160. end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  161. /* easy case */
  162. if (page->index < end_index)
  163. goto do_it;
  164. /* things got complicated... */
  165. offset = inode->i_size & (PAGE_CACHE_SIZE-1);
  166. /* OK, are we completely out? */
  167. if (page->index >= end_index+1 || !offset)
  168. return 0; /* truncated - don't care */
  169. do_it:
  170. page_cache_get(page);
  171. err = smb_writepage_sync(inode, page, 0, offset);
  172. SetPageUptodate(page);
  173. unlock_page(page);
  174. page_cache_release(page);
  175. return err;
  176. }
  177. static int
  178. smb_updatepage(struct file *file, struct page *page, unsigned long offset,
  179. unsigned int count)
  180. {
  181. struct dentry *dentry = file->f_path.dentry;
  182. DEBUG1("(%s/%s %d@%lld)\n", DENTRY_PATH(dentry), count,
  183. ((unsigned long long)page->index << PAGE_CACHE_SHIFT) + offset);
  184. return smb_writepage_sync(dentry->d_inode, page, offset, count);
  185. }
  186. static ssize_t
  187. smb_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  188. unsigned long nr_segs, loff_t pos)
  189. {
  190. struct file * file = iocb->ki_filp;
  191. struct dentry * dentry = file->f_path.dentry;
  192. ssize_t status;
  193. VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry),
  194. (unsigned long) iocb->ki_left, (unsigned long) pos);
  195. status = smb_revalidate_inode(dentry);
  196. if (status) {
  197. PARANOIA("%s/%s validation failed, error=%Zd\n",
  198. DENTRY_PATH(dentry), status);
  199. goto out;
  200. }
  201. VERBOSE("before read, size=%ld, flags=%x, atime=%ld\n",
  202. (long)dentry->d_inode->i_size,
  203. dentry->d_inode->i_flags, dentry->d_inode->i_atime.tv_sec);
  204. status = generic_file_aio_read(iocb, iov, nr_segs, pos);
  205. out:
  206. return status;
  207. }
  208. static int
  209. smb_file_mmap(struct file * file, struct vm_area_struct * vma)
  210. {
  211. struct dentry * dentry = file->f_path.dentry;
  212. int status;
  213. VERBOSE("file %s/%s, address %lu - %lu\n",
  214. DENTRY_PATH(dentry), vma->vm_start, vma->vm_end);
  215. status = smb_revalidate_inode(dentry);
  216. if (status) {
  217. PARANOIA("%s/%s validation failed, error=%d\n",
  218. DENTRY_PATH(dentry), status);
  219. goto out;
  220. }
  221. status = generic_file_mmap(file, vma);
  222. out:
  223. return status;
  224. }
  225. static ssize_t
  226. smb_file_splice_read(struct file *file, loff_t *ppos,
  227. struct pipe_inode_info *pipe, size_t count,
  228. unsigned int flags)
  229. {
  230. struct dentry *dentry = file->f_path.dentry;
  231. ssize_t status;
  232. VERBOSE("file %s/%s, pos=%Ld, count=%lu\n",
  233. DENTRY_PATH(dentry), *ppos, count);
  234. status = smb_revalidate_inode(dentry);
  235. if (status) {
  236. PARANOIA("%s/%s validation failed, error=%Zd\n",
  237. DENTRY_PATH(dentry), status);
  238. goto out;
  239. }
  240. status = generic_file_splice_read(file, ppos, pipe, count, flags);
  241. out:
  242. return status;
  243. }
  244. /*
  245. * This does the "real" work of the write. The generic routine has
  246. * allocated the page, locked it, done all the page alignment stuff
  247. * calculations etc. Now we should just copy the data from user
  248. * space and write it back to the real medium..
  249. *
  250. * If the writer ends up delaying the write, the writer needs to
  251. * increment the page use counts until he is done with the page.
  252. */
  253. static int smb_write_begin(struct file *file, struct address_space *mapping,
  254. loff_t pos, unsigned len, unsigned flags,
  255. struct page **pagep, void **fsdata)
  256. {
  257. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  258. *pagep = grab_cache_page_write_begin(mapping, index, flags);
  259. if (!*pagep)
  260. return -ENOMEM;
  261. return 0;
  262. }
  263. static int smb_write_end(struct file *file, struct address_space *mapping,
  264. loff_t pos, unsigned len, unsigned copied,
  265. struct page *page, void *fsdata)
  266. {
  267. int status;
  268. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  269. lock_kernel();
  270. status = smb_updatepage(file, page, offset, copied);
  271. unlock_kernel();
  272. if (!status) {
  273. if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
  274. SetPageUptodate(page);
  275. status = copied;
  276. }
  277. unlock_page(page);
  278. page_cache_release(page);
  279. return status;
  280. }
  281. const struct address_space_operations smb_file_aops = {
  282. .readpage = smb_readpage,
  283. .writepage = smb_writepage,
  284. .write_begin = smb_write_begin,
  285. .write_end = smb_write_end,
  286. };
  287. /*
  288. * Write to a file (through the page cache).
  289. */
  290. static ssize_t
  291. smb_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  292. unsigned long nr_segs, loff_t pos)
  293. {
  294. struct file * file = iocb->ki_filp;
  295. struct dentry * dentry = file->f_path.dentry;
  296. ssize_t result;
  297. VERBOSE("file %s/%s, count=%lu@%lu\n",
  298. DENTRY_PATH(dentry),
  299. (unsigned long) iocb->ki_left, (unsigned long) pos);
  300. result = smb_revalidate_inode(dentry);
  301. if (result) {
  302. PARANOIA("%s/%s validation failed, error=%Zd\n",
  303. DENTRY_PATH(dentry), result);
  304. goto out;
  305. }
  306. result = smb_open(dentry, SMB_O_WRONLY);
  307. if (result)
  308. goto out;
  309. if (iocb->ki_left > 0) {
  310. result = generic_file_aio_write(iocb, iov, nr_segs, pos);
  311. VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
  312. (long) file->f_pos, (long) dentry->d_inode->i_size,
  313. dentry->d_inode->i_mtime.tv_sec,
  314. dentry->d_inode->i_atime.tv_sec);
  315. }
  316. out:
  317. return result;
  318. }
  319. static int
  320. smb_file_open(struct inode *inode, struct file * file)
  321. {
  322. int result;
  323. struct dentry *dentry = file->f_path.dentry;
  324. int smb_mode = (file->f_mode & O_ACCMODE) - 1;
  325. lock_kernel();
  326. result = smb_open(dentry, smb_mode);
  327. if (result)
  328. goto out;
  329. SMB_I(inode)->openers++;
  330. out:
  331. unlock_kernel();
  332. return result;
  333. }
  334. static int
  335. smb_file_release(struct inode *inode, struct file * file)
  336. {
  337. lock_kernel();
  338. if (!--SMB_I(inode)->openers) {
  339. /* We must flush any dirty pages now as we won't be able to
  340. write anything after close. mmap can trigger this.
  341. "openers" should perhaps include mmap'ers ... */
  342. filemap_write_and_wait(inode->i_mapping);
  343. smb_close(inode);
  344. }
  345. unlock_kernel();
  346. return 0;
  347. }
  348. /*
  349. * Check whether the required access is compatible with
  350. * an inode's permission. SMB doesn't recognize superuser
  351. * privileges, so we need our own check for this.
  352. */
  353. static int
  354. smb_file_permission(struct inode *inode, int mask)
  355. {
  356. int mode = inode->i_mode;
  357. int error = 0;
  358. VERBOSE("mode=%x, mask=%x\n", mode, mask);
  359. /* Look at user permissions */
  360. mode >>= 6;
  361. if (mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC))
  362. error = -EACCES;
  363. return error;
  364. }
  365. static loff_t smb_remote_llseek(struct file *file, loff_t offset, int origin)
  366. {
  367. loff_t ret;
  368. lock_kernel();
  369. ret = generic_file_llseek_unlocked(file, offset, origin);
  370. unlock_kernel();
  371. return ret;
  372. }
  373. const struct file_operations smb_file_operations =
  374. {
  375. .llseek = smb_remote_llseek,
  376. .read = do_sync_read,
  377. .aio_read = smb_file_aio_read,
  378. .write = do_sync_write,
  379. .aio_write = smb_file_aio_write,
  380. .ioctl = smb_ioctl,
  381. .mmap = smb_file_mmap,
  382. .open = smb_file_open,
  383. .release = smb_file_release,
  384. .fsync = smb_fsync,
  385. .splice_read = smb_file_splice_read,
  386. };
  387. const struct inode_operations smb_file_inode_operations =
  388. {
  389. .permission = smb_file_permission,
  390. .getattr = smb_getattr,
  391. .setattr = smb_notify_change,
  392. };