inode.c 27 KB

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