inode.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. * inode.c
  3. *
  4. * Copyright (C) 1995, 1996 by Volker Lendecke
  5. * Modified for big endian by J.F. Chadima and David S. Miller
  6. * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
  7. * Modified 1998 Wolfram Pienkoss for NLS
  8. * Modified 2000 Ben Harris, University of Cambridge for NFS NS meta-info
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/byteorder.h>
  14. #include <linux/time.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/string.h>
  18. #include <linux/stat.h>
  19. #include <linux/errno.h>
  20. #include <linux/file.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/init.h>
  25. #include <linux/vfs.h>
  26. #include <linux/mount.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/namei.h>
  29. #include <net/sock.h>
  30. #include "ncp_fs.h"
  31. #include "getopt.h"
  32. #define NCP_DEFAULT_FILE_MODE 0600
  33. #define NCP_DEFAULT_DIR_MODE 0700
  34. #define NCP_DEFAULT_TIME_OUT 10
  35. #define NCP_DEFAULT_RETRY_COUNT 20
  36. static void ncp_evict_inode(struct inode *);
  37. static void ncp_put_super(struct super_block *);
  38. static int ncp_statfs(struct dentry *, struct kstatfs *);
  39. static int ncp_show_options(struct seq_file *, struct dentry *);
  40. static struct kmem_cache * ncp_inode_cachep;
  41. static struct inode *ncp_alloc_inode(struct super_block *sb)
  42. {
  43. struct ncp_inode_info *ei;
  44. ei = (struct ncp_inode_info *)kmem_cache_alloc(ncp_inode_cachep, GFP_KERNEL);
  45. if (!ei)
  46. return NULL;
  47. return &ei->vfs_inode;
  48. }
  49. static void ncp_i_callback(struct rcu_head *head)
  50. {
  51. struct inode *inode = container_of(head, struct inode, i_rcu);
  52. kmem_cache_free(ncp_inode_cachep, NCP_FINFO(inode));
  53. }
  54. static void ncp_destroy_inode(struct inode *inode)
  55. {
  56. call_rcu(&inode->i_rcu, ncp_i_callback);
  57. }
  58. static void init_once(void *foo)
  59. {
  60. struct ncp_inode_info *ei = (struct ncp_inode_info *) foo;
  61. mutex_init(&ei->open_mutex);
  62. inode_init_once(&ei->vfs_inode);
  63. }
  64. static int init_inodecache(void)
  65. {
  66. ncp_inode_cachep = kmem_cache_create("ncp_inode_cache",
  67. sizeof(struct ncp_inode_info),
  68. 0, (SLAB_RECLAIM_ACCOUNT|
  69. SLAB_MEM_SPREAD),
  70. init_once);
  71. if (ncp_inode_cachep == NULL)
  72. return -ENOMEM;
  73. return 0;
  74. }
  75. static void destroy_inodecache(void)
  76. {
  77. kmem_cache_destroy(ncp_inode_cachep);
  78. }
  79. static int ncp_remount(struct super_block *sb, int *flags, char* data)
  80. {
  81. sync_filesystem(sb);
  82. *flags |= MS_NODIRATIME;
  83. return 0;
  84. }
  85. static const struct super_operations ncp_sops =
  86. {
  87. .alloc_inode = ncp_alloc_inode,
  88. .destroy_inode = ncp_destroy_inode,
  89. .drop_inode = generic_delete_inode,
  90. .evict_inode = ncp_evict_inode,
  91. .put_super = ncp_put_super,
  92. .statfs = ncp_statfs,
  93. .remount_fs = ncp_remount,
  94. .show_options = ncp_show_options,
  95. };
  96. /*
  97. * Fill in the ncpfs-specific information in the inode.
  98. */
  99. static void ncp_update_dirent(struct inode *inode, struct ncp_entry_info *nwinfo)
  100. {
  101. NCP_FINFO(inode)->DosDirNum = nwinfo->i.DosDirNum;
  102. NCP_FINFO(inode)->dirEntNum = nwinfo->i.dirEntNum;
  103. NCP_FINFO(inode)->volNumber = nwinfo->volume;
  104. }
  105. void ncp_update_inode(struct inode *inode, struct ncp_entry_info *nwinfo)
  106. {
  107. ncp_update_dirent(inode, nwinfo);
  108. NCP_FINFO(inode)->nwattr = nwinfo->i.attributes;
  109. NCP_FINFO(inode)->access = nwinfo->access;
  110. memcpy(NCP_FINFO(inode)->file_handle, nwinfo->file_handle,
  111. sizeof(nwinfo->file_handle));
  112. DPRINTK("ncp_update_inode: updated %s, volnum=%d, dirent=%u\n",
  113. nwinfo->i.entryName, NCP_FINFO(inode)->volNumber,
  114. NCP_FINFO(inode)->dirEntNum);
  115. }
  116. static void ncp_update_dates(struct inode *inode, struct nw_info_struct *nwi)
  117. {
  118. /* NFS namespace mode overrides others if it's set. */
  119. DPRINTK(KERN_DEBUG "ncp_update_dates_and_mode: (%s) nfs.mode=0%o\n",
  120. nwi->entryName, nwi->nfs.mode);
  121. if (nwi->nfs.mode) {
  122. /* XXX Security? */
  123. inode->i_mode = nwi->nfs.mode;
  124. }
  125. inode->i_blocks = (i_size_read(inode) + NCP_BLOCK_SIZE - 1) >> NCP_BLOCK_SHIFT;
  126. inode->i_mtime.tv_sec = ncp_date_dos2unix(nwi->modifyTime, nwi->modifyDate);
  127. inode->i_ctime.tv_sec = ncp_date_dos2unix(nwi->creationTime, nwi->creationDate);
  128. inode->i_atime.tv_sec = ncp_date_dos2unix(0, nwi->lastAccessDate);
  129. inode->i_atime.tv_nsec = 0;
  130. inode->i_mtime.tv_nsec = 0;
  131. inode->i_ctime.tv_nsec = 0;
  132. }
  133. static void ncp_update_attrs(struct inode *inode, struct ncp_entry_info *nwinfo)
  134. {
  135. struct nw_info_struct *nwi = &nwinfo->i;
  136. struct ncp_server *server = NCP_SERVER(inode);
  137. if (nwi->attributes & aDIR) {
  138. inode->i_mode = server->m.dir_mode;
  139. /* for directories dataStreamSize seems to be some
  140. Object ID ??? */
  141. i_size_write(inode, NCP_BLOCK_SIZE);
  142. } else {
  143. u32 size;
  144. inode->i_mode = server->m.file_mode;
  145. size = le32_to_cpu(nwi->dataStreamSize);
  146. i_size_write(inode, size);
  147. #ifdef CONFIG_NCPFS_EXTRAS
  148. if ((server->m.flags & (NCP_MOUNT_EXTRAS|NCP_MOUNT_SYMLINKS))
  149. && (nwi->attributes & aSHARED)) {
  150. switch (nwi->attributes & (aHIDDEN|aSYSTEM)) {
  151. case aHIDDEN:
  152. if (server->m.flags & NCP_MOUNT_SYMLINKS) {
  153. if (/* (size >= NCP_MIN_SYMLINK_SIZE)
  154. && */ (size <= NCP_MAX_SYMLINK_SIZE)) {
  155. inode->i_mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
  156. NCP_FINFO(inode)->flags |= NCPI_KLUDGE_SYMLINK;
  157. break;
  158. }
  159. }
  160. /* FALLTHROUGH */
  161. case 0:
  162. if (server->m.flags & NCP_MOUNT_EXTRAS)
  163. inode->i_mode |= S_IRUGO;
  164. break;
  165. case aSYSTEM:
  166. if (server->m.flags & NCP_MOUNT_EXTRAS)
  167. inode->i_mode |= (inode->i_mode >> 2) & S_IXUGO;
  168. break;
  169. /* case aSYSTEM|aHIDDEN: */
  170. default:
  171. /* reserved combination */
  172. break;
  173. }
  174. }
  175. #endif
  176. }
  177. if (nwi->attributes & aRONLY) inode->i_mode &= ~S_IWUGO;
  178. }
  179. void ncp_update_inode2(struct inode* inode, struct ncp_entry_info *nwinfo)
  180. {
  181. NCP_FINFO(inode)->flags = 0;
  182. if (!atomic_read(&NCP_FINFO(inode)->opened)) {
  183. NCP_FINFO(inode)->nwattr = nwinfo->i.attributes;
  184. ncp_update_attrs(inode, nwinfo);
  185. }
  186. ncp_update_dates(inode, &nwinfo->i);
  187. ncp_update_dirent(inode, nwinfo);
  188. }
  189. /*
  190. * Fill in the inode based on the ncp_entry_info structure. Used only for brand new inodes.
  191. */
  192. static void ncp_set_attr(struct inode *inode, struct ncp_entry_info *nwinfo)
  193. {
  194. struct ncp_server *server = NCP_SERVER(inode);
  195. NCP_FINFO(inode)->flags = 0;
  196. ncp_update_attrs(inode, nwinfo);
  197. DDPRINTK("ncp_read_inode: inode->i_mode = %u\n", inode->i_mode);
  198. set_nlink(inode, 1);
  199. inode->i_uid = server->m.uid;
  200. inode->i_gid = server->m.gid;
  201. ncp_update_dates(inode, &nwinfo->i);
  202. ncp_update_inode(inode, nwinfo);
  203. }
  204. #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
  205. static const struct inode_operations ncp_symlink_inode_operations = {
  206. .readlink = generic_readlink,
  207. .follow_link = page_follow_link_light,
  208. .put_link = page_put_link,
  209. .setattr = ncp_notify_change,
  210. };
  211. #endif
  212. /*
  213. * Get a new inode.
  214. */
  215. struct inode *
  216. ncp_iget(struct super_block *sb, struct ncp_entry_info *info)
  217. {
  218. struct inode *inode;
  219. if (info == NULL) {
  220. printk(KERN_ERR "ncp_iget: info is NULL\n");
  221. return NULL;
  222. }
  223. inode = new_inode(sb);
  224. if (inode) {
  225. atomic_set(&NCP_FINFO(inode)->opened, info->opened);
  226. inode->i_mapping->backing_dev_info = sb->s_bdi;
  227. inode->i_ino = info->ino;
  228. ncp_set_attr(inode, info);
  229. if (S_ISREG(inode->i_mode)) {
  230. inode->i_op = &ncp_file_inode_operations;
  231. inode->i_fop = &ncp_file_operations;
  232. } else if (S_ISDIR(inode->i_mode)) {
  233. inode->i_op = &ncp_dir_inode_operations;
  234. inode->i_fop = &ncp_dir_operations;
  235. #ifdef CONFIG_NCPFS_NFS_NS
  236. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  237. init_special_inode(inode, inode->i_mode,
  238. new_decode_dev(info->i.nfs.rdev));
  239. #endif
  240. #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
  241. } else if (S_ISLNK(inode->i_mode)) {
  242. inode->i_op = &ncp_symlink_inode_operations;
  243. inode->i_data.a_ops = &ncp_symlink_aops;
  244. #endif
  245. } else {
  246. make_bad_inode(inode);
  247. }
  248. insert_inode_hash(inode);
  249. } else
  250. printk(KERN_ERR "ncp_iget: iget failed!\n");
  251. return inode;
  252. }
  253. static void
  254. ncp_evict_inode(struct inode *inode)
  255. {
  256. truncate_inode_pages(&inode->i_data, 0);
  257. end_writeback(inode);
  258. if (S_ISDIR(inode->i_mode)) {
  259. DDPRINTK("ncp_evict_inode: put directory %ld\n", inode->i_ino);
  260. }
  261. if (ncp_make_closed(inode) != 0) {
  262. /* We can't do anything but complain. */
  263. printk(KERN_ERR "ncp_evict_inode: could not close\n");
  264. }
  265. }
  266. static void ncp_stop_tasks(struct ncp_server *server) {
  267. struct sock* sk = server->ncp_sock->sk;
  268. lock_sock(sk);
  269. sk->sk_error_report = server->error_report;
  270. sk->sk_data_ready = server->data_ready;
  271. sk->sk_write_space = server->write_space;
  272. release_sock(sk);
  273. del_timer_sync(&server->timeout_tm);
  274. flush_work_sync(&server->rcv.tq);
  275. if (sk->sk_socket->type == SOCK_STREAM)
  276. flush_work_sync(&server->tx.tq);
  277. else
  278. flush_work_sync(&server->timeout_tq);
  279. }
  280. static int ncp_show_options(struct seq_file *seq, struct dentry *root)
  281. {
  282. struct ncp_server *server = NCP_SBP(root->d_sb);
  283. unsigned int tmp;
  284. if (server->m.uid != 0)
  285. seq_printf(seq, ",uid=%u", server->m.uid);
  286. if (server->m.gid != 0)
  287. seq_printf(seq, ",gid=%u", server->m.gid);
  288. if (server->m.mounted_uid != 0)
  289. seq_printf(seq, ",owner=%u", server->m.mounted_uid);
  290. tmp = server->m.file_mode & S_IALLUGO;
  291. if (tmp != NCP_DEFAULT_FILE_MODE)
  292. seq_printf(seq, ",mode=0%o", tmp);
  293. tmp = server->m.dir_mode & S_IALLUGO;
  294. if (tmp != NCP_DEFAULT_DIR_MODE)
  295. seq_printf(seq, ",dirmode=0%o", tmp);
  296. if (server->m.time_out != NCP_DEFAULT_TIME_OUT * HZ / 100) {
  297. tmp = server->m.time_out * 100 / HZ;
  298. seq_printf(seq, ",timeout=%u", tmp);
  299. }
  300. if (server->m.retry_count != NCP_DEFAULT_RETRY_COUNT)
  301. seq_printf(seq, ",retry=%u", server->m.retry_count);
  302. if (server->m.flags != 0)
  303. seq_printf(seq, ",flags=%lu", server->m.flags);
  304. if (server->m.wdog_pid != NULL)
  305. seq_printf(seq, ",wdogpid=%u", pid_vnr(server->m.wdog_pid));
  306. return 0;
  307. }
  308. static const struct ncp_option ncp_opts[] = {
  309. { "uid", OPT_INT, 'u' },
  310. { "gid", OPT_INT, 'g' },
  311. { "owner", OPT_INT, 'o' },
  312. { "mode", OPT_INT, 'm' },
  313. { "dirmode", OPT_INT, 'd' },
  314. { "timeout", OPT_INT, 't' },
  315. { "retry", OPT_INT, 'r' },
  316. { "flags", OPT_INT, 'f' },
  317. { "wdogpid", OPT_INT, 'w' },
  318. { "ncpfd", OPT_INT, 'n' },
  319. { "infofd", OPT_INT, 'i' }, /* v5 */
  320. { "version", OPT_INT, 'v' },
  321. { NULL, 0, 0 } };
  322. static int ncp_parse_options(struct ncp_mount_data_kernel *data, char *options) {
  323. int optval;
  324. char *optarg;
  325. unsigned long optint;
  326. int version = 0;
  327. int ret;
  328. data->flags = 0;
  329. data->int_flags = 0;
  330. data->mounted_uid = 0;
  331. data->wdog_pid = NULL;
  332. data->ncp_fd = ~0;
  333. data->time_out = NCP_DEFAULT_TIME_OUT;
  334. data->retry_count = NCP_DEFAULT_RETRY_COUNT;
  335. data->uid = 0;
  336. data->gid = 0;
  337. data->file_mode = NCP_DEFAULT_FILE_MODE;
  338. data->dir_mode = NCP_DEFAULT_DIR_MODE;
  339. data->info_fd = -1;
  340. data->mounted_vol[0] = 0;
  341. while ((optval = ncp_getopt("ncpfs", &options, ncp_opts, NULL, &optarg, &optint)) != 0) {
  342. ret = optval;
  343. if (ret < 0)
  344. goto err;
  345. switch (optval) {
  346. case 'u':
  347. data->uid = optint;
  348. break;
  349. case 'g':
  350. data->gid = optint;
  351. break;
  352. case 'o':
  353. data->mounted_uid = optint;
  354. break;
  355. case 'm':
  356. data->file_mode = optint;
  357. break;
  358. case 'd':
  359. data->dir_mode = optint;
  360. break;
  361. case 't':
  362. data->time_out = optint;
  363. break;
  364. case 'r':
  365. data->retry_count = optint;
  366. break;
  367. case 'f':
  368. data->flags = optint;
  369. break;
  370. case 'w':
  371. data->wdog_pid = find_get_pid(optint);
  372. break;
  373. case 'n':
  374. data->ncp_fd = optint;
  375. break;
  376. case 'i':
  377. data->info_fd = optint;
  378. break;
  379. case 'v':
  380. ret = -ECHRNG;
  381. if (optint < NCP_MOUNT_VERSION_V4)
  382. goto err;
  383. if (optint > NCP_MOUNT_VERSION_V5)
  384. goto err;
  385. version = optint;
  386. break;
  387. }
  388. }
  389. return 0;
  390. err:
  391. put_pid(data->wdog_pid);
  392. data->wdog_pid = NULL;
  393. return ret;
  394. }
  395. static int ncp_fill_super(struct super_block *sb, void *raw_data, int silent)
  396. {
  397. struct ncp_mount_data_kernel data;
  398. struct ncp_server *server;
  399. struct file *ncp_filp;
  400. struct inode *root_inode;
  401. struct inode *sock_inode;
  402. struct socket *sock;
  403. int error;
  404. int default_bufsize;
  405. #ifdef CONFIG_NCPFS_PACKET_SIGNING
  406. int options;
  407. #endif
  408. struct ncp_entry_info finfo;
  409. memset(&data, 0, sizeof(data));
  410. server = kzalloc(sizeof(struct ncp_server), GFP_KERNEL);
  411. if (!server)
  412. return -ENOMEM;
  413. sb->s_fs_info = server;
  414. error = -EFAULT;
  415. if (raw_data == NULL)
  416. goto out;
  417. switch (*(int*)raw_data) {
  418. case NCP_MOUNT_VERSION:
  419. {
  420. struct ncp_mount_data* md = (struct ncp_mount_data*)raw_data;
  421. data.flags = md->flags;
  422. data.int_flags = NCP_IMOUNT_LOGGEDIN_POSSIBLE;
  423. data.mounted_uid = md->mounted_uid;
  424. data.wdog_pid = find_get_pid(md->wdog_pid);
  425. data.ncp_fd = md->ncp_fd;
  426. data.time_out = md->time_out;
  427. data.retry_count = md->retry_count;
  428. data.uid = md->uid;
  429. data.gid = md->gid;
  430. data.file_mode = md->file_mode;
  431. data.dir_mode = md->dir_mode;
  432. data.info_fd = -1;
  433. memcpy(data.mounted_vol, md->mounted_vol,
  434. NCP_VOLNAME_LEN+1);
  435. }
  436. break;
  437. case NCP_MOUNT_VERSION_V4:
  438. {
  439. struct ncp_mount_data_v4* md = (struct ncp_mount_data_v4*)raw_data;
  440. data.flags = md->flags;
  441. data.mounted_uid = md->mounted_uid;
  442. data.wdog_pid = find_get_pid(md->wdog_pid);
  443. data.ncp_fd = md->ncp_fd;
  444. data.time_out = md->time_out;
  445. data.retry_count = md->retry_count;
  446. data.uid = md->uid;
  447. data.gid = md->gid;
  448. data.file_mode = md->file_mode;
  449. data.dir_mode = md->dir_mode;
  450. data.info_fd = -1;
  451. }
  452. break;
  453. default:
  454. error = -ECHRNG;
  455. if (memcmp(raw_data, "vers", 4) == 0) {
  456. error = ncp_parse_options(&data, raw_data);
  457. }
  458. if (error)
  459. goto out;
  460. break;
  461. }
  462. error = -EBADF;
  463. ncp_filp = fget(data.ncp_fd);
  464. if (!ncp_filp)
  465. goto out;
  466. error = -ENOTSOCK;
  467. sock_inode = ncp_filp->f_path.dentry->d_inode;
  468. if (!S_ISSOCK(sock_inode->i_mode))
  469. goto out_fput;
  470. sock = SOCKET_I(sock_inode);
  471. if (!sock)
  472. goto out_fput;
  473. if (sock->type == SOCK_STREAM)
  474. default_bufsize = 0xF000;
  475. else
  476. default_bufsize = 1024;
  477. sb->s_flags |= MS_NODIRATIME; /* probably even noatime */
  478. sb->s_maxbytes = 0xFFFFFFFFU;
  479. sb->s_blocksize = 1024; /* Eh... Is this correct? */
  480. sb->s_blocksize_bits = 10;
  481. sb->s_magic = NCP_SUPER_MAGIC;
  482. sb->s_op = &ncp_sops;
  483. sb->s_d_op = &ncp_dentry_operations;
  484. sb->s_bdi = &server->bdi;
  485. server = NCP_SBP(sb);
  486. memset(server, 0, sizeof(*server));
  487. error = bdi_setup_and_register(&server->bdi, "ncpfs", BDI_CAP_MAP_COPY);
  488. if (error)
  489. goto out_fput;
  490. server->ncp_filp = ncp_filp;
  491. server->ncp_sock = sock;
  492. if (data.info_fd != -1) {
  493. struct socket *info_sock;
  494. error = -EBADF;
  495. server->info_filp = fget(data.info_fd);
  496. if (!server->info_filp)
  497. goto out_bdi;
  498. error = -ENOTSOCK;
  499. sock_inode = server->info_filp->f_path.dentry->d_inode;
  500. if (!S_ISSOCK(sock_inode->i_mode))
  501. goto out_fput2;
  502. info_sock = SOCKET_I(sock_inode);
  503. if (!info_sock)
  504. goto out_fput2;
  505. error = -EBADFD;
  506. if (info_sock->type != SOCK_STREAM)
  507. goto out_fput2;
  508. server->info_sock = info_sock;
  509. }
  510. /* server->lock = 0; */
  511. mutex_init(&server->mutex);
  512. server->packet = NULL;
  513. /* server->buffer_size = 0; */
  514. /* server->conn_status = 0; */
  515. /* server->root_dentry = NULL; */
  516. /* server->root_setuped = 0; */
  517. mutex_init(&server->root_setup_lock);
  518. #ifdef CONFIG_NCPFS_PACKET_SIGNING
  519. /* server->sign_wanted = 0; */
  520. /* server->sign_active = 0; */
  521. #endif
  522. init_rwsem(&server->auth_rwsem);
  523. server->auth.auth_type = NCP_AUTH_NONE;
  524. /* server->auth.object_name_len = 0; */
  525. /* server->auth.object_name = NULL; */
  526. /* server->auth.object_type = 0; */
  527. /* server->priv.len = 0; */
  528. /* server->priv.data = NULL; */
  529. server->m = data;
  530. /* Although anything producing this is buggy, it happens
  531. now because of PATH_MAX changes.. */
  532. if (server->m.time_out < 1) {
  533. server->m.time_out = 10;
  534. printk(KERN_INFO "You need to recompile your ncpfs utils..\n");
  535. }
  536. server->m.time_out = server->m.time_out * HZ / 100;
  537. server->m.file_mode = (server->m.file_mode & S_IRWXUGO) | S_IFREG;
  538. server->m.dir_mode = (server->m.dir_mode & S_IRWXUGO) | S_IFDIR;
  539. #ifdef CONFIG_NCPFS_NLS
  540. /* load the default NLS charsets */
  541. server->nls_vol = load_nls_default();
  542. server->nls_io = load_nls_default();
  543. #endif /* CONFIG_NCPFS_NLS */
  544. atomic_set(&server->dentry_ttl, 0); /* no caching */
  545. INIT_LIST_HEAD(&server->tx.requests);
  546. mutex_init(&server->rcv.creq_mutex);
  547. server->tx.creq = NULL;
  548. server->rcv.creq = NULL;
  549. init_timer(&server->timeout_tm);
  550. #undef NCP_PACKET_SIZE
  551. #define NCP_PACKET_SIZE 131072
  552. error = -ENOMEM;
  553. server->packet_size = NCP_PACKET_SIZE;
  554. server->packet = vmalloc(NCP_PACKET_SIZE);
  555. if (server->packet == NULL)
  556. goto out_nls;
  557. server->txbuf = vmalloc(NCP_PACKET_SIZE);
  558. if (server->txbuf == NULL)
  559. goto out_packet;
  560. server->rxbuf = vmalloc(NCP_PACKET_SIZE);
  561. if (server->rxbuf == NULL)
  562. goto out_txbuf;
  563. lock_sock(sock->sk);
  564. server->data_ready = sock->sk->sk_data_ready;
  565. server->write_space = sock->sk->sk_write_space;
  566. server->error_report = sock->sk->sk_error_report;
  567. sock->sk->sk_user_data = server;
  568. sock->sk->sk_data_ready = ncp_tcp_data_ready;
  569. sock->sk->sk_error_report = ncp_tcp_error_report;
  570. if (sock->type == SOCK_STREAM) {
  571. server->rcv.ptr = (unsigned char*)&server->rcv.buf;
  572. server->rcv.len = 10;
  573. server->rcv.state = 0;
  574. INIT_WORK(&server->rcv.tq, ncp_tcp_rcv_proc);
  575. INIT_WORK(&server->tx.tq, ncp_tcp_tx_proc);
  576. sock->sk->sk_write_space = ncp_tcp_write_space;
  577. } else {
  578. INIT_WORK(&server->rcv.tq, ncpdgram_rcv_proc);
  579. INIT_WORK(&server->timeout_tq, ncpdgram_timeout_proc);
  580. server->timeout_tm.data = (unsigned long)server;
  581. server->timeout_tm.function = ncpdgram_timeout_call;
  582. }
  583. release_sock(sock->sk);
  584. ncp_lock_server(server);
  585. error = ncp_connect(server);
  586. ncp_unlock_server(server);
  587. if (error < 0)
  588. goto out_rxbuf;
  589. DPRINTK("ncp_fill_super: NCP_SBP(sb) = %x\n", (int) NCP_SBP(sb));
  590. error = -EMSGSIZE; /* -EREMOTESIDEINCOMPATIBLE */
  591. #ifdef CONFIG_NCPFS_PACKET_SIGNING
  592. if (ncp_negotiate_size_and_options(server, default_bufsize,
  593. NCP_DEFAULT_OPTIONS, &(server->buffer_size), &options) == 0)
  594. {
  595. if (options != NCP_DEFAULT_OPTIONS)
  596. {
  597. if (ncp_negotiate_size_and_options(server,
  598. default_bufsize,
  599. options & 2,
  600. &(server->buffer_size), &options) != 0)
  601. {
  602. goto out_disconnect;
  603. }
  604. }
  605. ncp_lock_server(server);
  606. if (options & 2)
  607. server->sign_wanted = 1;
  608. ncp_unlock_server(server);
  609. }
  610. else
  611. #endif /* CONFIG_NCPFS_PACKET_SIGNING */
  612. if (ncp_negotiate_buffersize(server, default_bufsize,
  613. &(server->buffer_size)) != 0)
  614. goto out_disconnect;
  615. DPRINTK("ncpfs: bufsize = %d\n", server->buffer_size);
  616. memset(&finfo, 0, sizeof(finfo));
  617. finfo.i.attributes = aDIR;
  618. finfo.i.dataStreamSize = 0; /* ignored */
  619. finfo.i.dirEntNum = 0;
  620. finfo.i.DosDirNum = 0;
  621. #ifdef CONFIG_NCPFS_SMALLDOS
  622. finfo.i.NSCreator = NW_NS_DOS;
  623. #endif
  624. finfo.volume = NCP_NUMBER_OF_VOLUMES;
  625. /* set dates of mountpoint to Jan 1, 1986; 00:00 */
  626. finfo.i.creationTime = finfo.i.modifyTime
  627. = cpu_to_le16(0x0000);
  628. finfo.i.creationDate = finfo.i.modifyDate
  629. = finfo.i.lastAccessDate
  630. = cpu_to_le16(0x0C21);
  631. finfo.i.nameLen = 0;
  632. finfo.i.entryName[0] = '\0';
  633. finfo.opened = 0;
  634. finfo.ino = 2; /* tradition */
  635. server->name_space[finfo.volume] = NW_NS_DOS;
  636. error = -ENOMEM;
  637. root_inode = ncp_iget(sb, &finfo);
  638. if (!root_inode)
  639. goto out_disconnect;
  640. DPRINTK("ncp_fill_super: root vol=%d\n", NCP_FINFO(root_inode)->volNumber);
  641. sb->s_root = d_make_root(root_inode);
  642. if (!sb->s_root)
  643. goto out_disconnect;
  644. return 0;
  645. out_disconnect:
  646. ncp_lock_server(server);
  647. ncp_disconnect(server);
  648. ncp_unlock_server(server);
  649. out_rxbuf:
  650. ncp_stop_tasks(server);
  651. vfree(server->rxbuf);
  652. out_txbuf:
  653. vfree(server->txbuf);
  654. out_packet:
  655. vfree(server->packet);
  656. out_nls:
  657. #ifdef CONFIG_NCPFS_NLS
  658. unload_nls(server->nls_io);
  659. unload_nls(server->nls_vol);
  660. #endif
  661. mutex_destroy(&server->rcv.creq_mutex);
  662. mutex_destroy(&server->root_setup_lock);
  663. mutex_destroy(&server->mutex);
  664. out_fput2:
  665. if (server->info_filp)
  666. fput(server->info_filp);
  667. out_bdi:
  668. bdi_destroy(&server->bdi);
  669. out_fput:
  670. /* 23/12/1998 Marcin Dalecki <dalecki@cs.net.pl>:
  671. *
  672. * The previously used put_filp(ncp_filp); was bogus, since
  673. * it doesn't perform proper unlocking.
  674. */
  675. fput(ncp_filp);
  676. out:
  677. put_pid(data.wdog_pid);
  678. sb->s_fs_info = NULL;
  679. kfree(server);
  680. return error;
  681. }
  682. static void ncp_put_super(struct super_block *sb)
  683. {
  684. struct ncp_server *server = NCP_SBP(sb);
  685. ncp_lock_server(server);
  686. ncp_disconnect(server);
  687. ncp_unlock_server(server);
  688. ncp_stop_tasks(server);
  689. #ifdef CONFIG_NCPFS_NLS
  690. /* unload the NLS charsets */
  691. unload_nls(server->nls_vol);
  692. unload_nls(server->nls_io);
  693. #endif /* CONFIG_NCPFS_NLS */
  694. mutex_destroy(&server->rcv.creq_mutex);
  695. mutex_destroy(&server->root_setup_lock);
  696. mutex_destroy(&server->mutex);
  697. if (server->info_filp)
  698. fput(server->info_filp);
  699. fput(server->ncp_filp);
  700. kill_pid(server->m.wdog_pid, SIGTERM, 1);
  701. put_pid(server->m.wdog_pid);
  702. bdi_destroy(&server->bdi);
  703. kfree(server->priv.data);
  704. kfree(server->auth.object_name);
  705. vfree(server->rxbuf);
  706. vfree(server->txbuf);
  707. vfree(server->packet);
  708. sb->s_fs_info = NULL;
  709. kfree(server);
  710. }
  711. static int ncp_statfs(struct dentry *dentry, struct kstatfs *buf)
  712. {
  713. struct dentry* d;
  714. struct inode* i;
  715. struct ncp_inode_info* ni;
  716. struct ncp_server* s;
  717. struct ncp_volume_info vi;
  718. struct super_block *sb = dentry->d_sb;
  719. int err;
  720. __u8 dh;
  721. d = sb->s_root;
  722. if (!d) {
  723. goto dflt;
  724. }
  725. i = d->d_inode;
  726. if (!i) {
  727. goto dflt;
  728. }
  729. ni = NCP_FINFO(i);
  730. if (!ni) {
  731. goto dflt;
  732. }
  733. s = NCP_SBP(sb);
  734. if (!s) {
  735. goto dflt;
  736. }
  737. if (!s->m.mounted_vol[0]) {
  738. goto dflt;
  739. }
  740. err = ncp_dirhandle_alloc(s, ni->volNumber, ni->DosDirNum, &dh);
  741. if (err) {
  742. goto dflt;
  743. }
  744. err = ncp_get_directory_info(s, dh, &vi);
  745. ncp_dirhandle_free(s, dh);
  746. if (err) {
  747. goto dflt;
  748. }
  749. buf->f_type = NCP_SUPER_MAGIC;
  750. buf->f_bsize = vi.sectors_per_block * 512;
  751. buf->f_blocks = vi.total_blocks;
  752. buf->f_bfree = vi.free_blocks;
  753. buf->f_bavail = vi.free_blocks;
  754. buf->f_files = vi.total_dir_entries;
  755. buf->f_ffree = vi.available_dir_entries;
  756. buf->f_namelen = 12;
  757. return 0;
  758. /* We cannot say how much disk space is left on a mounted
  759. NetWare Server, because free space is distributed over
  760. volumes, and the current user might have disk quotas. So
  761. free space is not that simple to determine. Our decision
  762. here is to err conservatively. */
  763. dflt:;
  764. buf->f_type = NCP_SUPER_MAGIC;
  765. buf->f_bsize = NCP_BLOCK_SIZE;
  766. buf->f_blocks = 0;
  767. buf->f_bfree = 0;
  768. buf->f_bavail = 0;
  769. buf->f_namelen = 12;
  770. return 0;
  771. }
  772. int ncp_notify_change(struct dentry *dentry, struct iattr *attr)
  773. {
  774. struct inode *inode = dentry->d_inode;
  775. int result = 0;
  776. __le32 info_mask;
  777. struct nw_modify_dos_info info;
  778. struct ncp_server *server;
  779. result = -EIO;
  780. server = NCP_SERVER(inode);
  781. if (!server) /* How this could happen? */
  782. goto out;
  783. /* ageing the dentry to force validation */
  784. ncp_age_dentry(server, dentry);
  785. result = inode_change_ok(inode, attr);
  786. if (result < 0)
  787. goto out;
  788. result = -EPERM;
  789. if (((attr->ia_valid & ATTR_UID) &&
  790. (attr->ia_uid != server->m.uid)))
  791. goto out;
  792. if (((attr->ia_valid & ATTR_GID) &&
  793. (attr->ia_gid != server->m.gid)))
  794. goto out;
  795. if (((attr->ia_valid & ATTR_MODE) &&
  796. (attr->ia_mode &
  797. ~(S_IFREG | S_IFDIR | S_IRWXUGO))))
  798. goto out;
  799. info_mask = 0;
  800. memset(&info, 0, sizeof(info));
  801. #if 1
  802. if ((attr->ia_valid & ATTR_MODE) != 0)
  803. {
  804. umode_t newmode = attr->ia_mode;
  805. info_mask |= DM_ATTRIBUTES;
  806. if (S_ISDIR(inode->i_mode)) {
  807. newmode &= server->m.dir_mode;
  808. } else {
  809. #ifdef CONFIG_NCPFS_EXTRAS
  810. if (server->m.flags & NCP_MOUNT_EXTRAS) {
  811. /* any non-default execute bit set */
  812. if (newmode & ~server->m.file_mode & S_IXUGO)
  813. info.attributes |= aSHARED | aSYSTEM;
  814. /* read for group/world and not in default file_mode */
  815. else if (newmode & ~server->m.file_mode & S_IRUGO)
  816. info.attributes |= aSHARED;
  817. } else
  818. #endif
  819. newmode &= server->m.file_mode;
  820. }
  821. if (newmode & S_IWUGO)
  822. info.attributes &= ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
  823. else
  824. info.attributes |= (aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
  825. #ifdef CONFIG_NCPFS_NFS_NS
  826. if (ncp_is_nfs_extras(server, NCP_FINFO(inode)->volNumber)) {
  827. result = ncp_modify_nfs_info(server,
  828. NCP_FINFO(inode)->volNumber,
  829. NCP_FINFO(inode)->dirEntNum,
  830. attr->ia_mode, 0);
  831. if (result != 0)
  832. goto out;
  833. info.attributes &= ~(aSHARED | aSYSTEM);
  834. {
  835. /* mark partial success */
  836. struct iattr tmpattr;
  837. tmpattr.ia_valid = ATTR_MODE;
  838. tmpattr.ia_mode = attr->ia_mode;
  839. setattr_copy(inode, &tmpattr);
  840. mark_inode_dirty(inode);
  841. }
  842. }
  843. #endif
  844. }
  845. #endif
  846. /* Do SIZE before attributes, otherwise mtime together with size does not work...
  847. */
  848. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  849. int written;
  850. DPRINTK("ncpfs: trying to change size to %ld\n",
  851. attr->ia_size);
  852. if ((result = ncp_make_open(inode, O_WRONLY)) < 0) {
  853. result = -EACCES;
  854. goto out;
  855. }
  856. ncp_write_kernel(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
  857. attr->ia_size, 0, "", &written);
  858. /* According to ndir, the changes only take effect after
  859. closing the file */
  860. ncp_inode_close(inode);
  861. result = ncp_make_closed(inode);
  862. if (result)
  863. goto out;
  864. if (attr->ia_size != i_size_read(inode)) {
  865. result = vmtruncate(inode, attr->ia_size);
  866. if (result)
  867. goto out;
  868. mark_inode_dirty(inode);
  869. }
  870. }
  871. if ((attr->ia_valid & ATTR_CTIME) != 0) {
  872. info_mask |= (DM_CREATE_TIME | DM_CREATE_DATE);
  873. ncp_date_unix2dos(attr->ia_ctime.tv_sec,
  874. &info.creationTime, &info.creationDate);
  875. }
  876. if ((attr->ia_valid & ATTR_MTIME) != 0) {
  877. info_mask |= (DM_MODIFY_TIME | DM_MODIFY_DATE);
  878. ncp_date_unix2dos(attr->ia_mtime.tv_sec,
  879. &info.modifyTime, &info.modifyDate);
  880. }
  881. if ((attr->ia_valid & ATTR_ATIME) != 0) {
  882. __le16 dummy;
  883. info_mask |= (DM_LAST_ACCESS_DATE);
  884. ncp_date_unix2dos(attr->ia_atime.tv_sec,
  885. &dummy, &info.lastAccessDate);
  886. }
  887. if (info_mask != 0) {
  888. result = ncp_modify_file_or_subdir_dos_info(NCP_SERVER(inode),
  889. inode, info_mask, &info);
  890. if (result != 0) {
  891. if (info_mask == (DM_CREATE_TIME | DM_CREATE_DATE)) {
  892. /* NetWare seems not to allow this. I
  893. do not know why. So, just tell the
  894. user everything went fine. This is
  895. a terrible hack, but I do not know
  896. how to do this correctly. */
  897. result = 0;
  898. } else
  899. goto out;
  900. }
  901. #ifdef CONFIG_NCPFS_STRONG
  902. if ((!result) && (info_mask & DM_ATTRIBUTES))
  903. NCP_FINFO(inode)->nwattr = info.attributes;
  904. #endif
  905. }
  906. if (result)
  907. goto out;
  908. setattr_copy(inode, attr);
  909. mark_inode_dirty(inode);
  910. out:
  911. if (result > 0)
  912. result = -EACCES;
  913. return result;
  914. }
  915. static struct dentry *ncp_mount(struct file_system_type *fs_type,
  916. int flags, const char *dev_name, void *data)
  917. {
  918. return mount_nodev(fs_type, flags, data, ncp_fill_super);
  919. }
  920. static struct file_system_type ncp_fs_type = {
  921. .owner = THIS_MODULE,
  922. .name = "ncpfs",
  923. .mount = ncp_mount,
  924. .kill_sb = kill_anon_super,
  925. .fs_flags = FS_BINARY_MOUNTDATA,
  926. };
  927. MODULE_ALIAS_FS("ncpfs");
  928. static int __init init_ncp_fs(void)
  929. {
  930. int err;
  931. DPRINTK("ncpfs: init_ncp_fs called\n");
  932. err = init_inodecache();
  933. if (err)
  934. goto out1;
  935. err = register_filesystem(&ncp_fs_type);
  936. if (err)
  937. goto out;
  938. return 0;
  939. out:
  940. destroy_inodecache();
  941. out1:
  942. return err;
  943. }
  944. static void __exit exit_ncp_fs(void)
  945. {
  946. DPRINTK("ncpfs: exit_ncp_fs called\n");
  947. unregister_filesystem(&ncp_fs_type);
  948. destroy_inodecache();
  949. }
  950. module_init(init_ncp_fs)
  951. module_exit(exit_ncp_fs)
  952. MODULE_LICENSE("GPL");