inode.c 20 KB

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