inode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * inode.c
  3. *
  4. * Copyright (C) 1995, 1996 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/module.h>
  10. #include <linux/time.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/string.h>
  14. #include <linux/stat.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #include <linux/init.h>
  18. #include <linux/file.h>
  19. #include <linux/dcache.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/nls.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/mount.h>
  24. #include <linux/net.h>
  25. #include <linux/vfs.h>
  26. #include <linux/highuid.h>
  27. #include <linux/smb_fs.h>
  28. #include <linux/smbno.h>
  29. #include <linux/smb_mount.h>
  30. #include <asm/system.h>
  31. #include <asm/uaccess.h>
  32. #include "smb_debug.h"
  33. #include "getopt.h"
  34. #include "proto.h"
  35. /* Always pick a default string */
  36. #ifdef CONFIG_SMB_NLS_REMOTE
  37. #define SMB_NLS_REMOTE CONFIG_SMB_NLS_REMOTE
  38. #else
  39. #define SMB_NLS_REMOTE ""
  40. #endif
  41. #define SMB_TTL_DEFAULT 1000
  42. static void smb_delete_inode(struct inode *);
  43. static void smb_put_super(struct super_block *);
  44. static int smb_statfs(struct dentry *, struct kstatfs *);
  45. static int smb_show_options(struct seq_file *, struct vfsmount *);
  46. static struct kmem_cache *smb_inode_cachep;
  47. static struct inode *smb_alloc_inode(struct super_block *sb)
  48. {
  49. struct smb_inode_info *ei;
  50. ei = (struct smb_inode_info *)kmem_cache_alloc(smb_inode_cachep, GFP_KERNEL);
  51. if (!ei)
  52. return NULL;
  53. return &ei->vfs_inode;
  54. }
  55. static void smb_destroy_inode(struct inode *inode)
  56. {
  57. kmem_cache_free(smb_inode_cachep, SMB_I(inode));
  58. }
  59. static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
  60. {
  61. struct smb_inode_info *ei = (struct smb_inode_info *) foo;
  62. inode_init_once(&ei->vfs_inode);
  63. }
  64. static int init_inodecache(void)
  65. {
  66. smb_inode_cachep = kmem_cache_create("smb_inode_cache",
  67. sizeof(struct smb_inode_info),
  68. 0, (SLAB_RECLAIM_ACCOUNT|
  69. SLAB_MEM_SPREAD),
  70. init_once, NULL);
  71. if (smb_inode_cachep == NULL)
  72. return -ENOMEM;
  73. return 0;
  74. }
  75. static void destroy_inodecache(void)
  76. {
  77. kmem_cache_destroy(smb_inode_cachep);
  78. }
  79. static int smb_remount(struct super_block *sb, int *flags, char *data)
  80. {
  81. *flags |= MS_NODIRATIME;
  82. return 0;
  83. }
  84. static const struct super_operations smb_sops =
  85. {
  86. .alloc_inode = smb_alloc_inode,
  87. .destroy_inode = smb_destroy_inode,
  88. .drop_inode = generic_delete_inode,
  89. .delete_inode = smb_delete_inode,
  90. .put_super = smb_put_super,
  91. .statfs = smb_statfs,
  92. .show_options = smb_show_options,
  93. .remount_fs = smb_remount,
  94. };
  95. /* We are always generating a new inode here */
  96. struct inode *
  97. smb_iget(struct super_block *sb, struct smb_fattr *fattr)
  98. {
  99. struct smb_sb_info *server = SMB_SB(sb);
  100. struct inode *result;
  101. DEBUG1("smb_iget: %p\n", fattr);
  102. result = new_inode(sb);
  103. if (!result)
  104. return result;
  105. result->i_ino = fattr->f_ino;
  106. SMB_I(result)->open = 0;
  107. SMB_I(result)->fileid = 0;
  108. SMB_I(result)->access = 0;
  109. SMB_I(result)->flags = 0;
  110. SMB_I(result)->closed = 0;
  111. SMB_I(result)->openers = 0;
  112. smb_set_inode_attr(result, fattr);
  113. if (S_ISREG(result->i_mode)) {
  114. result->i_op = &smb_file_inode_operations;
  115. result->i_fop = &smb_file_operations;
  116. result->i_data.a_ops = &smb_file_aops;
  117. } else if (S_ISDIR(result->i_mode)) {
  118. if (server->opt.capabilities & SMB_CAP_UNIX)
  119. result->i_op = &smb_dir_inode_operations_unix;
  120. else
  121. result->i_op = &smb_dir_inode_operations;
  122. result->i_fop = &smb_dir_operations;
  123. } else if (S_ISLNK(result->i_mode)) {
  124. result->i_op = &smb_link_inode_operations;
  125. } else {
  126. init_special_inode(result, result->i_mode, fattr->f_rdev);
  127. }
  128. insert_inode_hash(result);
  129. return result;
  130. }
  131. /*
  132. * Copy the inode data to a smb_fattr structure.
  133. */
  134. void
  135. smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
  136. {
  137. memset(fattr, 0, sizeof(struct smb_fattr));
  138. fattr->f_mode = inode->i_mode;
  139. fattr->f_nlink = inode->i_nlink;
  140. fattr->f_ino = inode->i_ino;
  141. fattr->f_uid = inode->i_uid;
  142. fattr->f_gid = inode->i_gid;
  143. fattr->f_size = inode->i_size;
  144. fattr->f_mtime = inode->i_mtime;
  145. fattr->f_ctime = inode->i_ctime;
  146. fattr->f_atime = inode->i_atime;
  147. fattr->f_blocks = inode->i_blocks;
  148. fattr->attr = SMB_I(inode)->attr;
  149. /*
  150. * Keep the attributes in sync with the inode permissions.
  151. */
  152. if (fattr->f_mode & S_IWUSR)
  153. fattr->attr &= ~aRONLY;
  154. else
  155. fattr->attr |= aRONLY;
  156. }
  157. /*
  158. * Update the inode, possibly causing it to invalidate its pages if mtime/size
  159. * is different from last time.
  160. */
  161. void
  162. smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
  163. {
  164. struct smb_inode_info *ei = SMB_I(inode);
  165. /*
  166. * A size change should have a different mtime, or same mtime
  167. * but different size.
  168. */
  169. time_t last_time = inode->i_mtime.tv_sec;
  170. loff_t last_sz = inode->i_size;
  171. inode->i_mode = fattr->f_mode;
  172. inode->i_nlink = fattr->f_nlink;
  173. inode->i_uid = fattr->f_uid;
  174. inode->i_gid = fattr->f_gid;
  175. inode->i_ctime = fattr->f_ctime;
  176. inode->i_blocks = fattr->f_blocks;
  177. inode->i_size = fattr->f_size;
  178. inode->i_mtime = fattr->f_mtime;
  179. inode->i_atime = fattr->f_atime;
  180. ei->attr = fattr->attr;
  181. /*
  182. * Update the "last time refreshed" field for revalidation.
  183. */
  184. ei->oldmtime = jiffies;
  185. if (inode->i_mtime.tv_sec != last_time || inode->i_size != last_sz) {
  186. VERBOSE("%ld changed, old=%ld, new=%ld, oz=%ld, nz=%ld\n",
  187. inode->i_ino,
  188. (long) last_time, (long) inode->i_mtime.tv_sec,
  189. (long) last_sz, (long) inode->i_size);
  190. if (!S_ISDIR(inode->i_mode))
  191. invalidate_remote_inode(inode);
  192. }
  193. }
  194. /*
  195. * This is called if the connection has gone bad ...
  196. * try to kill off all the current inodes.
  197. */
  198. void
  199. smb_invalidate_inodes(struct smb_sb_info *server)
  200. {
  201. VERBOSE("\n");
  202. shrink_dcache_sb(SB_of(server));
  203. invalidate_inodes(SB_of(server));
  204. }
  205. /*
  206. * This is called to update the inode attributes after
  207. * we've made changes to a file or directory.
  208. */
  209. static int
  210. smb_refresh_inode(struct dentry *dentry)
  211. {
  212. struct inode *inode = dentry->d_inode;
  213. int error;
  214. struct smb_fattr fattr;
  215. error = smb_proc_getattr(dentry, &fattr);
  216. if (!error) {
  217. smb_renew_times(dentry);
  218. /*
  219. * Check whether the type part of the mode changed,
  220. * and don't update the attributes if it did.
  221. *
  222. * And don't dick with the root inode
  223. */
  224. if (inode->i_ino == 2)
  225. return error;
  226. if (S_ISLNK(inode->i_mode))
  227. return error; /* VFS will deal with it */
  228. if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT)) {
  229. smb_set_inode_attr(inode, &fattr);
  230. } else {
  231. /*
  232. * Big trouble! The inode has become a new object,
  233. * so any operations attempted on it are invalid.
  234. *
  235. * To limit damage, mark the inode as bad so that
  236. * subsequent lookup validations will fail.
  237. */
  238. PARANOIA("%s/%s changed mode, %07o to %07o\n",
  239. DENTRY_PATH(dentry),
  240. inode->i_mode, fattr.f_mode);
  241. fattr.f_mode = inode->i_mode; /* save mode */
  242. make_bad_inode(inode);
  243. inode->i_mode = fattr.f_mode; /* restore mode */
  244. /*
  245. * No need to worry about unhashing the dentry: the
  246. * lookup validation will see that the inode is bad.
  247. * But we do want to invalidate the caches ...
  248. */
  249. if (!S_ISDIR(inode->i_mode))
  250. invalidate_remote_inode(inode);
  251. else
  252. smb_invalid_dir_cache(inode);
  253. error = -EIO;
  254. }
  255. }
  256. return error;
  257. }
  258. /*
  259. * This is called when we want to check whether the inode
  260. * has changed on the server. If it has changed, we must
  261. * invalidate our local caches.
  262. */
  263. int
  264. smb_revalidate_inode(struct dentry *dentry)
  265. {
  266. struct smb_sb_info *s = server_from_dentry(dentry);
  267. struct inode *inode = dentry->d_inode;
  268. int error = 0;
  269. DEBUG1("smb_revalidate_inode\n");
  270. lock_kernel();
  271. /*
  272. * Check whether we've recently refreshed the inode.
  273. */
  274. if (time_before(jiffies, SMB_I(inode)->oldmtime + SMB_MAX_AGE(s))) {
  275. VERBOSE("up-to-date, ino=%ld, jiffies=%lu, oldtime=%lu\n",
  276. inode->i_ino, jiffies, SMB_I(inode)->oldmtime);
  277. goto out;
  278. }
  279. error = smb_refresh_inode(dentry);
  280. out:
  281. unlock_kernel();
  282. return error;
  283. }
  284. /*
  285. * This routine is called when i_nlink == 0 and i_count goes to 0.
  286. * All blocking cleanup operations need to go here to avoid races.
  287. */
  288. static void
  289. smb_delete_inode(struct inode *ino)
  290. {
  291. DEBUG1("ino=%ld\n", ino->i_ino);
  292. truncate_inode_pages(&ino->i_data, 0);
  293. lock_kernel();
  294. if (smb_close(ino))
  295. PARANOIA("could not close inode %ld\n", ino->i_ino);
  296. unlock_kernel();
  297. clear_inode(ino);
  298. }
  299. static struct option opts[] = {
  300. { "version", 0, 'v' },
  301. { "win95", SMB_MOUNT_WIN95, 1 },
  302. { "oldattr", SMB_MOUNT_OLDATTR, 1 },
  303. { "dirattr", SMB_MOUNT_DIRATTR, 1 },
  304. { "case", SMB_MOUNT_CASE, 1 },
  305. { "uid", 0, 'u' },
  306. { "gid", 0, 'g' },
  307. { "file_mode", 0, 'f' },
  308. { "dir_mode", 0, 'd' },
  309. { "iocharset", 0, 'i' },
  310. { "codepage", 0, 'c' },
  311. { "ttl", 0, 't' },
  312. { NULL, 0, 0}
  313. };
  314. static int
  315. parse_options(struct smb_mount_data_kernel *mnt, char *options)
  316. {
  317. int c;
  318. unsigned long flags;
  319. unsigned long value;
  320. char *optarg;
  321. char *optopt;
  322. flags = 0;
  323. while ( (c = smb_getopt("smbfs", &options, opts,
  324. &optopt, &optarg, &flags, &value)) > 0) {
  325. VERBOSE("'%s' -> '%s'\n", optopt, optarg ? optarg : "<none>");
  326. switch (c) {
  327. case 1:
  328. /* got a "flag" option */
  329. break;
  330. case 'v':
  331. if (value != SMB_MOUNT_VERSION) {
  332. printk ("smbfs: Bad mount version %ld, expected %d\n",
  333. value, SMB_MOUNT_VERSION);
  334. return 0;
  335. }
  336. mnt->version = value;
  337. break;
  338. case 'u':
  339. mnt->uid = value;
  340. flags |= SMB_MOUNT_UID;
  341. break;
  342. case 'g':
  343. mnt->gid = value;
  344. flags |= SMB_MOUNT_GID;
  345. break;
  346. case 'f':
  347. mnt->file_mode = (value & S_IRWXUGO) | S_IFREG;
  348. flags |= SMB_MOUNT_FMODE;
  349. break;
  350. case 'd':
  351. mnt->dir_mode = (value & S_IRWXUGO) | S_IFDIR;
  352. flags |= SMB_MOUNT_DMODE;
  353. break;
  354. case 'i':
  355. strlcpy(mnt->codepage.local_name, optarg,
  356. SMB_NLS_MAXNAMELEN);
  357. break;
  358. case 'c':
  359. strlcpy(mnt->codepage.remote_name, optarg,
  360. SMB_NLS_MAXNAMELEN);
  361. break;
  362. case 't':
  363. mnt->ttl = value;
  364. break;
  365. default:
  366. printk ("smbfs: Unrecognized mount option %s\n",
  367. optopt);
  368. return -1;
  369. }
  370. }
  371. mnt->flags = flags;
  372. return c;
  373. }
  374. /*
  375. * smb_show_options() is for displaying mount options in /proc/mounts.
  376. * It tries to avoid showing settings that were not changed from their
  377. * defaults.
  378. */
  379. static int
  380. smb_show_options(struct seq_file *s, struct vfsmount *m)
  381. {
  382. struct smb_mount_data_kernel *mnt = SMB_SB(m->mnt_sb)->mnt;
  383. int i;
  384. for (i = 0; opts[i].name != NULL; i++)
  385. if (mnt->flags & opts[i].flag)
  386. seq_printf(s, ",%s", opts[i].name);
  387. if (mnt->flags & SMB_MOUNT_UID)
  388. seq_printf(s, ",uid=%d", mnt->uid);
  389. if (mnt->flags & SMB_MOUNT_GID)
  390. seq_printf(s, ",gid=%d", mnt->gid);
  391. if (mnt->mounted_uid != 0)
  392. seq_printf(s, ",mounted_uid=%d", mnt->mounted_uid);
  393. /*
  394. * Defaults for file_mode and dir_mode are unknown to us; they
  395. * depend on the current umask of the user doing the mount.
  396. */
  397. if (mnt->flags & SMB_MOUNT_FMODE)
  398. seq_printf(s, ",file_mode=%04o", mnt->file_mode & S_IRWXUGO);
  399. if (mnt->flags & SMB_MOUNT_DMODE)
  400. seq_printf(s, ",dir_mode=%04o", mnt->dir_mode & S_IRWXUGO);
  401. if (strcmp(mnt->codepage.local_name, CONFIG_NLS_DEFAULT))
  402. seq_printf(s, ",iocharset=%s", mnt->codepage.local_name);
  403. if (strcmp(mnt->codepage.remote_name, SMB_NLS_REMOTE))
  404. seq_printf(s, ",codepage=%s", mnt->codepage.remote_name);
  405. if (mnt->ttl != SMB_TTL_DEFAULT)
  406. seq_printf(s, ",ttl=%d", mnt->ttl);
  407. return 0;
  408. }
  409. static void
  410. smb_unload_nls(struct smb_sb_info *server)
  411. {
  412. if (server->remote_nls) {
  413. unload_nls(server->remote_nls);
  414. server->remote_nls = NULL;
  415. }
  416. if (server->local_nls) {
  417. unload_nls(server->local_nls);
  418. server->local_nls = NULL;
  419. }
  420. }
  421. static void
  422. smb_put_super(struct super_block *sb)
  423. {
  424. struct smb_sb_info *server = SMB_SB(sb);
  425. smb_lock_server(server);
  426. server->state = CONN_INVALID;
  427. smbiod_unregister_server(server);
  428. smb_close_socket(server);
  429. if (server->conn_pid)
  430. kill_pid(server->conn_pid, SIGTERM, 1);
  431. kfree(server->ops);
  432. smb_unload_nls(server);
  433. sb->s_fs_info = NULL;
  434. smb_unlock_server(server);
  435. put_pid(server->conn_pid);
  436. kfree(server);
  437. }
  438. static int smb_fill_super(struct super_block *sb, void *raw_data, int silent)
  439. {
  440. struct smb_sb_info *server;
  441. struct smb_mount_data_kernel *mnt;
  442. struct smb_mount_data *oldmnt;
  443. struct inode *root_inode;
  444. struct smb_fattr root;
  445. int ver;
  446. void *mem;
  447. if (!raw_data)
  448. goto out_no_data;
  449. oldmnt = (struct smb_mount_data *) raw_data;
  450. ver = oldmnt->version;
  451. if (ver != SMB_MOUNT_OLDVERSION && cpu_to_be32(ver) != SMB_MOUNT_ASCII)
  452. goto out_wrong_data;
  453. sb->s_flags |= MS_NODIRATIME;
  454. sb->s_blocksize = 1024; /* Eh... Is this correct? */
  455. sb->s_blocksize_bits = 10;
  456. sb->s_magic = SMB_SUPER_MAGIC;
  457. sb->s_op = &smb_sops;
  458. sb->s_time_gran = 100;
  459. server = kzalloc(sizeof(struct smb_sb_info), GFP_KERNEL);
  460. if (!server)
  461. goto out_no_server;
  462. sb->s_fs_info = server;
  463. server->super_block = sb;
  464. server->mnt = NULL;
  465. server->sock_file = NULL;
  466. init_waitqueue_head(&server->conn_wq);
  467. init_MUTEX(&server->sem);
  468. INIT_LIST_HEAD(&server->entry);
  469. INIT_LIST_HEAD(&server->xmitq);
  470. INIT_LIST_HEAD(&server->recvq);
  471. server->conn_error = 0;
  472. server->conn_pid = NULL;
  473. server->state = CONN_INVALID; /* no connection yet */
  474. server->generation = 0;
  475. /* Allocate the global temp buffer and some superblock helper structs */
  476. /* FIXME: move these to the smb_sb_info struct */
  477. VERBOSE("alloc chunk = %d\n", sizeof(struct smb_ops) +
  478. sizeof(struct smb_mount_data_kernel));
  479. mem = kmalloc(sizeof(struct smb_ops) +
  480. sizeof(struct smb_mount_data_kernel), GFP_KERNEL);
  481. if (!mem)
  482. goto out_no_mem;
  483. server->ops = mem;
  484. smb_install_null_ops(server->ops);
  485. server->mnt = mem + sizeof(struct smb_ops);
  486. /* Setup NLS stuff */
  487. server->remote_nls = NULL;
  488. server->local_nls = NULL;
  489. mnt = server->mnt;
  490. memset(mnt, 0, sizeof(struct smb_mount_data_kernel));
  491. strlcpy(mnt->codepage.local_name, CONFIG_NLS_DEFAULT,
  492. SMB_NLS_MAXNAMELEN);
  493. strlcpy(mnt->codepage.remote_name, SMB_NLS_REMOTE,
  494. SMB_NLS_MAXNAMELEN);
  495. mnt->ttl = SMB_TTL_DEFAULT;
  496. if (ver == SMB_MOUNT_OLDVERSION) {
  497. mnt->version = oldmnt->version;
  498. SET_UID(mnt->uid, oldmnt->uid);
  499. SET_GID(mnt->gid, oldmnt->gid);
  500. mnt->file_mode = (oldmnt->file_mode & S_IRWXUGO) | S_IFREG;
  501. mnt->dir_mode = (oldmnt->dir_mode & S_IRWXUGO) | S_IFDIR;
  502. mnt->flags = (oldmnt->file_mode >> 9) | SMB_MOUNT_UID |
  503. SMB_MOUNT_GID | SMB_MOUNT_FMODE | SMB_MOUNT_DMODE;
  504. } else {
  505. mnt->file_mode = S_IRWXU | S_IRGRP | S_IXGRP |
  506. S_IROTH | S_IXOTH | S_IFREG;
  507. mnt->dir_mode = S_IRWXU | S_IRGRP | S_IXGRP |
  508. S_IROTH | S_IXOTH | S_IFDIR;
  509. if (parse_options(mnt, raw_data))
  510. goto out_bad_option;
  511. }
  512. mnt->mounted_uid = current->uid;
  513. smb_setcodepage(server, &mnt->codepage);
  514. /*
  515. * Display the enabled options
  516. * Note: smb_proc_getattr uses these in 2.4 (but was changed in 2.2)
  517. */
  518. if (mnt->flags & SMB_MOUNT_OLDATTR)
  519. printk("SMBFS: Using core getattr (Win 95 speedup)\n");
  520. else if (mnt->flags & SMB_MOUNT_DIRATTR)
  521. printk("SMBFS: Using dir ff getattr\n");
  522. if (smbiod_register_server(server) < 0) {
  523. printk(KERN_ERR "smbfs: failed to start smbiod\n");
  524. goto out_no_smbiod;
  525. }
  526. /*
  527. * Keep the super block locked while we get the root inode.
  528. */
  529. smb_init_root_dirent(server, &root, sb);
  530. root_inode = smb_iget(sb, &root);
  531. if (!root_inode)
  532. goto out_no_root;
  533. sb->s_root = d_alloc_root(root_inode);
  534. if (!sb->s_root)
  535. goto out_no_root;
  536. smb_new_dentry(sb->s_root);
  537. return 0;
  538. out_no_root:
  539. iput(root_inode);
  540. out_no_smbiod:
  541. smb_unload_nls(server);
  542. out_bad_option:
  543. kfree(mem);
  544. out_no_mem:
  545. if (!server->mnt)
  546. printk(KERN_ERR "smb_fill_super: allocation failure\n");
  547. sb->s_fs_info = NULL;
  548. kfree(server);
  549. goto out_fail;
  550. out_wrong_data:
  551. printk(KERN_ERR "smbfs: mount_data version %d is not supported\n", ver);
  552. goto out_fail;
  553. out_no_data:
  554. printk(KERN_ERR "smb_fill_super: missing data argument\n");
  555. out_fail:
  556. return -EINVAL;
  557. out_no_server:
  558. printk(KERN_ERR "smb_fill_super: cannot allocate struct smb_sb_info\n");
  559. return -ENOMEM;
  560. }
  561. static int
  562. smb_statfs(struct dentry *dentry, struct kstatfs *buf)
  563. {
  564. int result;
  565. lock_kernel();
  566. result = smb_proc_dskattr(dentry, buf);
  567. unlock_kernel();
  568. buf->f_type = SMB_SUPER_MAGIC;
  569. buf->f_namelen = SMB_MAXPATHLEN;
  570. return result;
  571. }
  572. int smb_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  573. {
  574. int err = smb_revalidate_inode(dentry);
  575. if (!err)
  576. generic_fillattr(dentry->d_inode, stat);
  577. return err;
  578. }
  579. int
  580. smb_notify_change(struct dentry *dentry, struct iattr *attr)
  581. {
  582. struct inode *inode = dentry->d_inode;
  583. struct smb_sb_info *server = server_from_dentry(dentry);
  584. unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXUGO);
  585. int error, changed, refresh = 0;
  586. struct smb_fattr fattr;
  587. lock_kernel();
  588. error = smb_revalidate_inode(dentry);
  589. if (error)
  590. goto out;
  591. if ((error = inode_change_ok(inode, attr)) < 0)
  592. goto out;
  593. error = -EPERM;
  594. if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
  595. goto out;
  596. if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
  597. goto out;
  598. if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
  599. goto out;
  600. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  601. VERBOSE("changing %s/%s, old size=%ld, new size=%ld\n",
  602. DENTRY_PATH(dentry),
  603. (long) inode->i_size, (long) attr->ia_size);
  604. filemap_write_and_wait(inode->i_mapping);
  605. error = smb_open(dentry, O_WRONLY);
  606. if (error)
  607. goto out;
  608. error = server->ops->truncate(inode, attr->ia_size);
  609. if (error)
  610. goto out;
  611. error = vmtruncate(inode, attr->ia_size);
  612. if (error)
  613. goto out;
  614. refresh = 1;
  615. }
  616. if (server->opt.capabilities & SMB_CAP_UNIX) {
  617. /* For now we don't want to set the size with setattr_unix */
  618. attr->ia_valid &= ~ATTR_SIZE;
  619. /* FIXME: only call if we actually want to set something? */
  620. error = smb_proc_setattr_unix(dentry, attr, 0, 0);
  621. if (!error)
  622. refresh = 1;
  623. goto out;
  624. }
  625. /*
  626. * Initialize the fattr and check for changed fields.
  627. * Note: CTIME under SMB is creation time rather than
  628. * change time, so we don't attempt to change it.
  629. */
  630. smb_get_inode_attr(inode, &fattr);
  631. changed = 0;
  632. if ((attr->ia_valid & ATTR_MTIME) != 0) {
  633. fattr.f_mtime = attr->ia_mtime;
  634. changed = 1;
  635. }
  636. if ((attr->ia_valid & ATTR_ATIME) != 0) {
  637. fattr.f_atime = attr->ia_atime;
  638. /* Earlier protocols don't have an access time */
  639. if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
  640. changed = 1;
  641. }
  642. if (changed) {
  643. error = smb_proc_settime(dentry, &fattr);
  644. if (error)
  645. goto out;
  646. refresh = 1;
  647. }
  648. /*
  649. * Check for mode changes ... we're extremely limited in
  650. * what can be set for SMB servers: just the read-only bit.
  651. */
  652. if ((attr->ia_valid & ATTR_MODE) != 0) {
  653. VERBOSE("%s/%s mode change, old=%x, new=%x\n",
  654. DENTRY_PATH(dentry), fattr.f_mode, attr->ia_mode);
  655. changed = 0;
  656. if (attr->ia_mode & S_IWUSR) {
  657. if (fattr.attr & aRONLY) {
  658. fattr.attr &= ~aRONLY;
  659. changed = 1;
  660. }
  661. } else {
  662. if (!(fattr.attr & aRONLY)) {
  663. fattr.attr |= aRONLY;
  664. changed = 1;
  665. }
  666. }
  667. if (changed) {
  668. error = smb_proc_setattr(dentry, &fattr);
  669. if (error)
  670. goto out;
  671. refresh = 1;
  672. }
  673. }
  674. error = 0;
  675. out:
  676. if (refresh)
  677. smb_refresh_inode(dentry);
  678. unlock_kernel();
  679. return error;
  680. }
  681. static int smb_get_sb(struct file_system_type *fs_type,
  682. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  683. {
  684. return get_sb_nodev(fs_type, flags, data, smb_fill_super, mnt);
  685. }
  686. static struct file_system_type smb_fs_type = {
  687. .owner = THIS_MODULE,
  688. .name = "smbfs",
  689. .get_sb = smb_get_sb,
  690. .kill_sb = kill_anon_super,
  691. .fs_flags = FS_BINARY_MOUNTDATA,
  692. };
  693. static int __init init_smb_fs(void)
  694. {
  695. int err;
  696. DEBUG1("registering ...\n");
  697. err = init_inodecache();
  698. if (err)
  699. goto out_inode;
  700. err = smb_init_request_cache();
  701. if (err)
  702. goto out_request;
  703. err = register_filesystem(&smb_fs_type);
  704. if (err)
  705. goto out;
  706. return 0;
  707. out:
  708. smb_destroy_request_cache();
  709. out_request:
  710. destroy_inodecache();
  711. out_inode:
  712. return err;
  713. }
  714. static void __exit exit_smb_fs(void)
  715. {
  716. DEBUG1("unregistering ...\n");
  717. unregister_filesystem(&smb_fs_type);
  718. smb_destroy_request_cache();
  719. destroy_inodecache();
  720. }
  721. module_init(init_smb_fs)
  722. module_exit(exit_smb_fs)
  723. MODULE_LICENSE("GPL");