file.c 10 KB

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