inode.c 20 KB

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