inode.c 20 KB

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