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