file.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. if (!mapping)
  157. BUG();
  158. inode = mapping->host;
  159. if (!inode)
  160. BUG();
  161. end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  162. /* easy case */
  163. if (page->index < end_index)
  164. goto do_it;
  165. /* things got complicated... */
  166. offset = inode->i_size & (PAGE_CACHE_SIZE-1);
  167. /* OK, are we completely out? */
  168. if (page->index >= end_index+1 || !offset)
  169. return 0; /* truncated - don't care */
  170. do_it:
  171. page_cache_get(page);
  172. err = smb_writepage_sync(inode, page, 0, offset);
  173. SetPageUptodate(page);
  174. unlock_page(page);
  175. page_cache_release(page);
  176. return err;
  177. }
  178. static int
  179. smb_updatepage(struct file *file, struct page *page, unsigned long offset,
  180. unsigned int count)
  181. {
  182. struct dentry *dentry = file->f_dentry;
  183. DEBUG1("(%s/%s %d@%ld)\n", DENTRY_PATH(dentry),
  184. count, (page->index << PAGE_CACHE_SHIFT)+offset);
  185. return smb_writepage_sync(dentry->d_inode, page, offset, count);
  186. }
  187. static ssize_t
  188. smb_file_read(struct file * file, char __user * buf, size_t count, loff_t *ppos)
  189. {
  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) count, (unsigned long) *ppos);
  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_read(file, buf, count, ppos);
  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. 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_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  277. {
  278. struct dentry * dentry = file->f_dentry;
  279. ssize_t result;
  280. VERBOSE("file %s/%s, count=%lu@%lu\n",
  281. DENTRY_PATH(dentry),
  282. (unsigned long) count, (unsigned long) *ppos);
  283. result = smb_revalidate_inode(dentry);
  284. if (result) {
  285. PARANOIA("%s/%s validation failed, error=%Zd\n",
  286. DENTRY_PATH(dentry), result);
  287. goto out;
  288. }
  289. result = smb_open(dentry, SMB_O_WRONLY);
  290. if (result)
  291. goto out;
  292. if (count > 0) {
  293. result = generic_file_write(file, buf, count, ppos);
  294. VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
  295. (long) file->f_pos, (long) dentry->d_inode->i_size,
  296. dentry->d_inode->i_mtime, dentry->d_inode->i_atime);
  297. }
  298. out:
  299. return result;
  300. }
  301. static int
  302. smb_file_open(struct inode *inode, struct file * file)
  303. {
  304. int result;
  305. struct dentry *dentry = file->f_dentry;
  306. int smb_mode = (file->f_mode & O_ACCMODE) - 1;
  307. lock_kernel();
  308. result = smb_open(dentry, smb_mode);
  309. if (result)
  310. goto out;
  311. SMB_I(inode)->openers++;
  312. out:
  313. unlock_kernel();
  314. return result;
  315. }
  316. static int
  317. smb_file_release(struct inode *inode, struct file * file)
  318. {
  319. lock_kernel();
  320. if (!--SMB_I(inode)->openers) {
  321. /* We must flush any dirty pages now as we won't be able to
  322. write anything after close. mmap can trigger this.
  323. "openers" should perhaps include mmap'ers ... */
  324. filemap_fdatawrite(inode->i_mapping);
  325. filemap_fdatawait(inode->i_mapping);
  326. smb_close(inode);
  327. }
  328. unlock_kernel();
  329. return 0;
  330. }
  331. /*
  332. * Check whether the required access is compatible with
  333. * an inode's permission. SMB doesn't recognize superuser
  334. * privileges, so we need our own check for this.
  335. */
  336. static int
  337. smb_file_permission(struct inode *inode, int mask, struct nameidata *nd)
  338. {
  339. int mode = inode->i_mode;
  340. int error = 0;
  341. VERBOSE("mode=%x, mask=%x\n", mode, mask);
  342. /* Look at user permissions */
  343. mode >>= 6;
  344. if ((mode & 7 & mask) != mask)
  345. error = -EACCES;
  346. return error;
  347. }
  348. struct file_operations smb_file_operations =
  349. {
  350. .llseek = remote_llseek,
  351. .read = smb_file_read,
  352. .write = smb_file_write,
  353. .ioctl = smb_ioctl,
  354. .mmap = smb_file_mmap,
  355. .open = smb_file_open,
  356. .release = smb_file_release,
  357. .fsync = smb_fsync,
  358. .sendfile = smb_file_sendfile,
  359. };
  360. struct inode_operations smb_file_inode_operations =
  361. {
  362. .permission = smb_file_permission,
  363. .getattr = smb_getattr,
  364. .setattr = smb_notify_change,
  365. };