inode.c 20 KB

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