inode.c 27 KB

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