file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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);
  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_sendfile(struct file *file, loff_t *ppos,
  227. size_t count, read_actor_t actor, void *target)
  228. {
  229. struct dentry *dentry = file->f_path.dentry;
  230. ssize_t status;
  231. VERBOSE("file %s/%s, pos=%Ld, count=%d\n",
  232. DENTRY_PATH(dentry), *ppos, count);
  233. status = smb_revalidate_inode(dentry);
  234. if (status) {
  235. PARANOIA("%s/%s validation failed, error=%Zd\n",
  236. DENTRY_PATH(dentry), status);
  237. goto out;
  238. }
  239. status = generic_file_sendfile(file, ppos, count, actor, target);
  240. out:
  241. return status;
  242. }
  243. /*
  244. * This does the "real" work of the write. The generic routine has
  245. * allocated the page, locked it, done all the page alignment stuff
  246. * calculations etc. Now we should just copy the data from user
  247. * space and write it back to the real medium..
  248. *
  249. * If the writer ends up delaying the write, the writer needs to
  250. * increment the page use counts until he is done with the page.
  251. */
  252. static int smb_prepare_write(struct file *file, struct page *page,
  253. unsigned offset, unsigned to)
  254. {
  255. return 0;
  256. }
  257. static int smb_commit_write(struct file *file, struct page *page,
  258. unsigned offset, unsigned to)
  259. {
  260. int status;
  261. status = -EFAULT;
  262. lock_kernel();
  263. status = smb_updatepage(file, page, offset, to-offset);
  264. unlock_kernel();
  265. return status;
  266. }
  267. const struct address_space_operations smb_file_aops = {
  268. .readpage = smb_readpage,
  269. .writepage = smb_writepage,
  270. .prepare_write = smb_prepare_write,
  271. .commit_write = smb_commit_write
  272. };
  273. /*
  274. * Write to a file (through the page cache).
  275. */
  276. static ssize_t
  277. smb_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  278. unsigned long nr_segs, loff_t pos)
  279. {
  280. struct file * file = iocb->ki_filp;
  281. struct dentry * dentry = file->f_path.dentry;
  282. ssize_t result;
  283. VERBOSE("file %s/%s, count=%lu@%lu\n",
  284. DENTRY_PATH(dentry),
  285. (unsigned long) iocb->ki_left, (unsigned long) pos);
  286. result = smb_revalidate_inode(dentry);
  287. if (result) {
  288. PARANOIA("%s/%s validation failed, error=%Zd\n",
  289. DENTRY_PATH(dentry), result);
  290. goto out;
  291. }
  292. result = smb_open(dentry, SMB_O_WRONLY);
  293. if (result)
  294. goto out;
  295. if (iocb->ki_left > 0) {
  296. result = generic_file_aio_write(iocb, iov, nr_segs, pos);
  297. VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
  298. (long) file->f_pos, (long) dentry->d_inode->i_size,
  299. dentry->d_inode->i_mtime, dentry->d_inode->i_atime);
  300. }
  301. out:
  302. return result;
  303. }
  304. static int
  305. smb_file_open(struct inode *inode, struct file * file)
  306. {
  307. int result;
  308. struct dentry *dentry = file->f_path.dentry;
  309. int smb_mode = (file->f_mode & O_ACCMODE) - 1;
  310. lock_kernel();
  311. result = smb_open(dentry, smb_mode);
  312. if (result)
  313. goto out;
  314. SMB_I(inode)->openers++;
  315. out:
  316. unlock_kernel();
  317. return result;
  318. }
  319. static int
  320. smb_file_release(struct inode *inode, struct file * file)
  321. {
  322. lock_kernel();
  323. if (!--SMB_I(inode)->openers) {
  324. /* We must flush any dirty pages now as we won't be able to
  325. write anything after close. mmap can trigger this.
  326. "openers" should perhaps include mmap'ers ... */
  327. filemap_write_and_wait(inode->i_mapping);
  328. smb_close(inode);
  329. }
  330. unlock_kernel();
  331. return 0;
  332. }
  333. /*
  334. * Check whether the required access is compatible with
  335. * an inode's permission. SMB doesn't recognize superuser
  336. * privileges, so we need our own check for this.
  337. */
  338. static int
  339. smb_file_permission(struct inode *inode, int mask, struct nameidata *nd)
  340. {
  341. int mode = inode->i_mode;
  342. int error = 0;
  343. VERBOSE("mode=%x, mask=%x\n", mode, mask);
  344. /* Look at user permissions */
  345. mode >>= 6;
  346. if ((mode & 7 & mask) != mask)
  347. error = -EACCES;
  348. return error;
  349. }
  350. const struct file_operations smb_file_operations =
  351. {
  352. .llseek = remote_llseek,
  353. .read = do_sync_read,
  354. .aio_read = smb_file_aio_read,
  355. .write = do_sync_write,
  356. .aio_write = smb_file_aio_write,
  357. .ioctl = smb_ioctl,
  358. .mmap = smb_file_mmap,
  359. .open = smb_file_open,
  360. .release = smb_file_release,
  361. .fsync = smb_fsync,
  362. .sendfile = smb_file_sendfile,
  363. };
  364. const struct inode_operations smb_file_inode_operations =
  365. {
  366. .permission = smb_file_permission,
  367. .getattr = smb_getattr,
  368. .setattr = smb_notify_change,
  369. };