file.c 9.9 KB

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