dir.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * dir.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, 1999 Wolfram Pienkoss for NLS
  8. * Modified 1999 Wolfram Pienkoss for directory caching
  9. * Modified 2000 Ben Harris, University of Cambridge for NFS NS meta-info
  10. *
  11. */
  12. #include <linux/time.h>
  13. #include <linux/errno.h>
  14. #include <linux/stat.h>
  15. #include <linux/kernel.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/mm.h>
  18. #include <linux/namei.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/byteorder.h>
  21. #include <linux/ncp_fs.h>
  22. #include "ncplib_kernel.h"
  23. static void ncp_read_volume_list(struct file *, void *, filldir_t,
  24. struct ncp_cache_control *);
  25. static void ncp_do_readdir(struct file *, void *, filldir_t,
  26. struct ncp_cache_control *);
  27. static int ncp_readdir(struct file *, void *, filldir_t);
  28. static int ncp_create(struct inode *, struct dentry *, int, struct nameidata *);
  29. static struct dentry *ncp_lookup(struct inode *, struct dentry *, struct nameidata *);
  30. static int ncp_unlink(struct inode *, struct dentry *);
  31. static int ncp_mkdir(struct inode *, struct dentry *, int);
  32. static int ncp_rmdir(struct inode *, struct dentry *);
  33. static int ncp_rename(struct inode *, struct dentry *,
  34. struct inode *, struct dentry *);
  35. static int ncp_mknod(struct inode * dir, struct dentry *dentry,
  36. int mode, dev_t rdev);
  37. #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
  38. extern int ncp_symlink(struct inode *, struct dentry *, const char *);
  39. #else
  40. #define ncp_symlink NULL
  41. #endif
  42. const struct file_operations ncp_dir_operations =
  43. {
  44. .llseek = generic_file_llseek,
  45. .read = generic_read_dir,
  46. .readdir = ncp_readdir,
  47. .unlocked_ioctl = ncp_ioctl,
  48. #ifdef CONFIG_COMPAT
  49. .compat_ioctl = ncp_compat_ioctl,
  50. #endif
  51. };
  52. const struct inode_operations ncp_dir_inode_operations =
  53. {
  54. .create = ncp_create,
  55. .lookup = ncp_lookup,
  56. .unlink = ncp_unlink,
  57. .symlink = ncp_symlink,
  58. .mkdir = ncp_mkdir,
  59. .rmdir = ncp_rmdir,
  60. .mknod = ncp_mknod,
  61. .rename = ncp_rename,
  62. .setattr = ncp_notify_change,
  63. };
  64. /*
  65. * Dentry operations routines
  66. */
  67. static int ncp_lookup_validate(struct dentry *, struct nameidata *);
  68. static int ncp_hash_dentry(const struct dentry *, const struct inode *,
  69. struct qstr *);
  70. static int ncp_compare_dentry(const struct dentry *, const struct inode *,
  71. const struct dentry *, const struct inode *,
  72. unsigned int, const char *, const struct qstr *);
  73. static int ncp_delete_dentry(const struct dentry *);
  74. static const struct dentry_operations ncp_dentry_operations =
  75. {
  76. .d_revalidate = ncp_lookup_validate,
  77. .d_hash = ncp_hash_dentry,
  78. .d_compare = ncp_compare_dentry,
  79. .d_delete = ncp_delete_dentry,
  80. };
  81. const struct dentry_operations ncp_root_dentry_operations =
  82. {
  83. .d_hash = ncp_hash_dentry,
  84. .d_compare = ncp_compare_dentry,
  85. .d_delete = ncp_delete_dentry,
  86. };
  87. #define ncp_namespace(i) (NCP_SERVER(i)->name_space[NCP_FINFO(i)->volNumber])
  88. static inline int ncp_preserve_entry_case(struct inode *i, __u32 nscreator)
  89. {
  90. #ifdef CONFIG_NCPFS_SMALLDOS
  91. int ns = ncp_namespace(i);
  92. if ((ns == NW_NS_DOS)
  93. #ifdef CONFIG_NCPFS_OS2_NS
  94. || ((ns == NW_NS_OS2) && (nscreator == NW_NS_DOS))
  95. #endif /* CONFIG_NCPFS_OS2_NS */
  96. )
  97. return 0;
  98. #endif /* CONFIG_NCPFS_SMALLDOS */
  99. return 1;
  100. }
  101. #define ncp_preserve_case(i) (ncp_namespace(i) != NW_NS_DOS)
  102. static inline int ncp_case_sensitive(const struct inode *i)
  103. {
  104. #ifdef CONFIG_NCPFS_NFS_NS
  105. return ncp_namespace(i) == NW_NS_NFS;
  106. #else
  107. return 0;
  108. #endif /* CONFIG_NCPFS_NFS_NS */
  109. }
  110. /*
  111. * Note: leave the hash unchanged if the directory
  112. * is case-sensitive.
  113. */
  114. static int
  115. ncp_hash_dentry(const struct dentry *dentry, const struct inode *inode,
  116. struct qstr *this)
  117. {
  118. if (!ncp_case_sensitive(inode)) {
  119. struct super_block *sb = dentry->d_sb;
  120. struct nls_table *t;
  121. unsigned long hash;
  122. int i;
  123. t = NCP_IO_TABLE(sb);
  124. hash = init_name_hash();
  125. for (i=0; i<this->len ; i++)
  126. hash = partial_name_hash(ncp_tolower(t, this->name[i]),
  127. hash);
  128. this->hash = end_name_hash(hash);
  129. }
  130. return 0;
  131. }
  132. static int
  133. ncp_compare_dentry(const struct dentry *parent, const struct inode *pinode,
  134. const struct dentry *dentry, const struct inode *inode,
  135. unsigned int len, const char *str, const struct qstr *name)
  136. {
  137. if (len != name->len)
  138. return 1;
  139. if (ncp_case_sensitive(pinode))
  140. return strncmp(str, name->name, len);
  141. return ncp_strnicmp(NCP_IO_TABLE(pinode->i_sb), str, name->name, len);
  142. }
  143. /*
  144. * This is the callback from dput() when d_count is going to 0.
  145. * We use this to unhash dentries with bad inodes.
  146. * Closing files can be safely postponed until iput() - it's done there anyway.
  147. */
  148. static int
  149. ncp_delete_dentry(const struct dentry * dentry)
  150. {
  151. struct inode *inode = dentry->d_inode;
  152. if (inode) {
  153. if (is_bad_inode(inode))
  154. return 1;
  155. } else
  156. {
  157. /* N.B. Unhash negative dentries? */
  158. }
  159. return 0;
  160. }
  161. static inline int
  162. ncp_single_volume(struct ncp_server *server)
  163. {
  164. return (server->m.mounted_vol[0] != '\0');
  165. }
  166. static inline int ncp_is_server_root(struct inode *inode)
  167. {
  168. return (!ncp_single_volume(NCP_SERVER(inode)) &&
  169. inode == inode->i_sb->s_root->d_inode);
  170. }
  171. /*
  172. * This is the callback when the dcache has a lookup hit.
  173. */
  174. #ifdef CONFIG_NCPFS_STRONG
  175. /* try to delete a readonly file (NW R bit set) */
  176. static int
  177. ncp_force_unlink(struct inode *dir, struct dentry* dentry)
  178. {
  179. int res=0x9c,res2;
  180. struct nw_modify_dos_info info;
  181. __le32 old_nwattr;
  182. struct inode *inode;
  183. memset(&info, 0, sizeof(info));
  184. /* remove the Read-Only flag on the NW server */
  185. inode = dentry->d_inode;
  186. old_nwattr = NCP_FINFO(inode)->nwattr;
  187. info.attributes = old_nwattr & ~(aRONLY|aDELETEINHIBIT|aRENAMEINHIBIT);
  188. res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info);
  189. if (res2)
  190. goto leave_me;
  191. /* now try again the delete operation */
  192. res = ncp_del_file_or_subdir2(NCP_SERVER(dir), dentry);
  193. if (res) /* delete failed, set R bit again */
  194. {
  195. info.attributes = old_nwattr;
  196. res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info);
  197. if (res2)
  198. goto leave_me;
  199. }
  200. leave_me:
  201. return(res);
  202. }
  203. #endif /* CONFIG_NCPFS_STRONG */
  204. #ifdef CONFIG_NCPFS_STRONG
  205. static int
  206. ncp_force_rename(struct inode *old_dir, struct dentry* old_dentry, char *_old_name,
  207. struct inode *new_dir, struct dentry* new_dentry, char *_new_name)
  208. {
  209. struct nw_modify_dos_info info;
  210. int res=0x90,res2;
  211. struct inode *old_inode = old_dentry->d_inode;
  212. __le32 old_nwattr = NCP_FINFO(old_inode)->nwattr;
  213. __le32 new_nwattr = 0; /* shut compiler warning */
  214. int old_nwattr_changed = 0;
  215. int new_nwattr_changed = 0;
  216. memset(&info, 0, sizeof(info));
  217. /* remove the Read-Only flag on the NW server */
  218. info.attributes = old_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
  219. res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info);
  220. if (!res2)
  221. old_nwattr_changed = 1;
  222. if (new_dentry && new_dentry->d_inode) {
  223. new_nwattr = NCP_FINFO(new_dentry->d_inode)->nwattr;
  224. info.attributes = new_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
  225. res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info);
  226. if (!res2)
  227. new_nwattr_changed = 1;
  228. }
  229. /* now try again the rename operation */
  230. /* but only if something really happened */
  231. if (new_nwattr_changed || old_nwattr_changed) {
  232. res = ncp_ren_or_mov_file_or_subdir(NCP_SERVER(old_dir),
  233. old_dir, _old_name,
  234. new_dir, _new_name);
  235. }
  236. if (res)
  237. goto leave_me;
  238. /* file was successfully renamed, so:
  239. do not set attributes on old file - it no longer exists
  240. copy attributes from old file to new */
  241. new_nwattr_changed = old_nwattr_changed;
  242. new_nwattr = old_nwattr;
  243. old_nwattr_changed = 0;
  244. leave_me:;
  245. if (old_nwattr_changed) {
  246. info.attributes = old_nwattr;
  247. res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info);
  248. /* ignore errors */
  249. }
  250. if (new_nwattr_changed) {
  251. info.attributes = new_nwattr;
  252. res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info);
  253. /* ignore errors */
  254. }
  255. return(res);
  256. }
  257. #endif /* CONFIG_NCPFS_STRONG */
  258. static int
  259. ncp_lookup_validate(struct dentry *dentry, struct nameidata *nd)
  260. {
  261. struct ncp_server *server;
  262. struct dentry *parent;
  263. struct inode *dir;
  264. struct ncp_entry_info finfo;
  265. int res, val = 0, len;
  266. __u8 __name[NCP_MAXPATHLEN + 1];
  267. if (nd->flags & LOOKUP_RCU)
  268. return -ECHILD;
  269. parent = dget_parent(dentry);
  270. dir = parent->d_inode;
  271. if (!dentry->d_inode)
  272. goto finished;
  273. server = NCP_SERVER(dir);
  274. /*
  275. * Inspired by smbfs:
  276. * The default validation is based on dentry age:
  277. * We set the max age at mount time. (But each
  278. * successful server lookup renews the timestamp.)
  279. */
  280. val = NCP_TEST_AGE(server, dentry);
  281. if (val)
  282. goto finished;
  283. DDPRINTK("ncp_lookup_validate: %s/%s not valid, age=%ld, server lookup\n",
  284. dentry->d_parent->d_name.name, dentry->d_name.name,
  285. NCP_GET_AGE(dentry));
  286. len = sizeof(__name);
  287. if (ncp_is_server_root(dir)) {
  288. res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
  289. dentry->d_name.len, 1);
  290. if (!res) {
  291. res = ncp_lookup_volume(server, __name, &(finfo.i));
  292. if (!res)
  293. ncp_update_known_namespace(server, finfo.i.volNumber, NULL);
  294. }
  295. } else {
  296. res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
  297. dentry->d_name.len, !ncp_preserve_case(dir));
  298. if (!res)
  299. res = ncp_obtain_info(server, dir, __name, &(finfo.i));
  300. }
  301. finfo.volume = finfo.i.volNumber;
  302. DDPRINTK("ncp_lookup_validate: looked for %s/%s, res=%d\n",
  303. dentry->d_parent->d_name.name, __name, res);
  304. /*
  305. * If we didn't find it, or if it has a different dirEntNum to
  306. * what we remember, it's not valid any more.
  307. */
  308. if (!res) {
  309. struct inode *inode = dentry->d_inode;
  310. mutex_lock(&inode->i_mutex);
  311. if (finfo.i.dirEntNum == NCP_FINFO(inode)->dirEntNum) {
  312. ncp_new_dentry(dentry);
  313. val=1;
  314. } else
  315. DDPRINTK("ncp_lookup_validate: found, but dirEntNum changed\n");
  316. ncp_update_inode2(inode, &finfo);
  317. mutex_unlock(&inode->i_mutex);
  318. }
  319. finished:
  320. DDPRINTK("ncp_lookup_validate: result=%d\n", val);
  321. dput(parent);
  322. return val;
  323. }
  324. static struct dentry *
  325. ncp_dget_fpos(struct dentry *dentry, struct dentry *parent, unsigned long fpos)
  326. {
  327. struct dentry *dent = dentry;
  328. struct list_head *next;
  329. if (d_validate(dent, parent)) {
  330. if (dent->d_name.len <= NCP_MAXPATHLEN &&
  331. (unsigned long)dent->d_fsdata == fpos) {
  332. if (!dent->d_inode) {
  333. dput(dent);
  334. dent = NULL;
  335. }
  336. return dent;
  337. }
  338. dput(dent);
  339. }
  340. /* If a pointer is invalid, we search the dentry. */
  341. spin_lock(&parent->d_lock);
  342. next = parent->d_subdirs.next;
  343. while (next != &parent->d_subdirs) {
  344. dent = list_entry(next, struct dentry, d_u.d_child);
  345. if ((unsigned long)dent->d_fsdata == fpos) {
  346. if (dent->d_inode)
  347. dget(dent);
  348. else
  349. dent = NULL;
  350. spin_unlock(&parent->d_lock);
  351. goto out;
  352. }
  353. next = next->next;
  354. }
  355. spin_unlock(&parent->d_lock);
  356. return NULL;
  357. out:
  358. return dent;
  359. }
  360. static time_t ncp_obtain_mtime(struct dentry *dentry)
  361. {
  362. struct inode *inode = dentry->d_inode;
  363. struct ncp_server *server = NCP_SERVER(inode);
  364. struct nw_info_struct i;
  365. if (!ncp_conn_valid(server) || ncp_is_server_root(inode))
  366. return 0;
  367. if (ncp_obtain_info(server, inode, NULL, &i))
  368. return 0;
  369. return ncp_date_dos2unix(i.modifyTime, i.modifyDate);
  370. }
  371. static int ncp_readdir(struct file *filp, void *dirent, filldir_t filldir)
  372. {
  373. struct dentry *dentry = filp->f_path.dentry;
  374. struct inode *inode = dentry->d_inode;
  375. struct page *page = NULL;
  376. struct ncp_server *server = NCP_SERVER(inode);
  377. union ncp_dir_cache *cache = NULL;
  378. struct ncp_cache_control ctl;
  379. int result, mtime_valid = 0;
  380. time_t mtime = 0;
  381. ctl.page = NULL;
  382. ctl.cache = NULL;
  383. DDPRINTK("ncp_readdir: reading %s/%s, pos=%d\n",
  384. dentry->d_parent->d_name.name, dentry->d_name.name,
  385. (int) filp->f_pos);
  386. result = -EIO;
  387. /* Do not generate '.' and '..' when server is dead. */
  388. if (!ncp_conn_valid(server))
  389. goto out;
  390. result = 0;
  391. if (filp->f_pos == 0) {
  392. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
  393. goto out;
  394. filp->f_pos = 1;
  395. }
  396. if (filp->f_pos == 1) {
  397. if (filldir(dirent, "..", 2, 1, parent_ino(dentry), DT_DIR))
  398. goto out;
  399. filp->f_pos = 2;
  400. }
  401. page = grab_cache_page(&inode->i_data, 0);
  402. if (!page)
  403. goto read_really;
  404. ctl.cache = cache = kmap(page);
  405. ctl.head = cache->head;
  406. if (!PageUptodate(page) || !ctl.head.eof)
  407. goto init_cache;
  408. if (filp->f_pos == 2) {
  409. if (jiffies - ctl.head.time >= NCP_MAX_AGE(server))
  410. goto init_cache;
  411. mtime = ncp_obtain_mtime(dentry);
  412. mtime_valid = 1;
  413. if ((!mtime) || (mtime != ctl.head.mtime))
  414. goto init_cache;
  415. }
  416. if (filp->f_pos > ctl.head.end)
  417. goto finished;
  418. ctl.fpos = filp->f_pos + (NCP_DIRCACHE_START - 2);
  419. ctl.ofs = ctl.fpos / NCP_DIRCACHE_SIZE;
  420. ctl.idx = ctl.fpos % NCP_DIRCACHE_SIZE;
  421. for (;;) {
  422. if (ctl.ofs != 0) {
  423. ctl.page = find_lock_page(&inode->i_data, ctl.ofs);
  424. if (!ctl.page)
  425. goto invalid_cache;
  426. ctl.cache = kmap(ctl.page);
  427. if (!PageUptodate(ctl.page))
  428. goto invalid_cache;
  429. }
  430. while (ctl.idx < NCP_DIRCACHE_SIZE) {
  431. struct dentry *dent;
  432. int res;
  433. dent = ncp_dget_fpos(ctl.cache->dentry[ctl.idx],
  434. dentry, filp->f_pos);
  435. if (!dent)
  436. goto invalid_cache;
  437. res = filldir(dirent, dent->d_name.name,
  438. dent->d_name.len, filp->f_pos,
  439. dent->d_inode->i_ino, DT_UNKNOWN);
  440. dput(dent);
  441. if (res)
  442. goto finished;
  443. filp->f_pos += 1;
  444. ctl.idx += 1;
  445. if (filp->f_pos > ctl.head.end)
  446. goto finished;
  447. }
  448. if (ctl.page) {
  449. kunmap(ctl.page);
  450. SetPageUptodate(ctl.page);
  451. unlock_page(ctl.page);
  452. page_cache_release(ctl.page);
  453. ctl.page = NULL;
  454. }
  455. ctl.idx = 0;
  456. ctl.ofs += 1;
  457. }
  458. invalid_cache:
  459. if (ctl.page) {
  460. kunmap(ctl.page);
  461. unlock_page(ctl.page);
  462. page_cache_release(ctl.page);
  463. ctl.page = NULL;
  464. }
  465. ctl.cache = cache;
  466. init_cache:
  467. ncp_invalidate_dircache_entries(dentry);
  468. if (!mtime_valid) {
  469. mtime = ncp_obtain_mtime(dentry);
  470. mtime_valid = 1;
  471. }
  472. ctl.head.mtime = mtime;
  473. ctl.head.time = jiffies;
  474. ctl.head.eof = 0;
  475. ctl.fpos = 2;
  476. ctl.ofs = 0;
  477. ctl.idx = NCP_DIRCACHE_START;
  478. ctl.filled = 0;
  479. ctl.valid = 1;
  480. read_really:
  481. if (ncp_is_server_root(inode)) {
  482. ncp_read_volume_list(filp, dirent, filldir, &ctl);
  483. } else {
  484. ncp_do_readdir(filp, dirent, filldir, &ctl);
  485. }
  486. ctl.head.end = ctl.fpos - 1;
  487. ctl.head.eof = ctl.valid;
  488. finished:
  489. if (ctl.page) {
  490. kunmap(ctl.page);
  491. SetPageUptodate(ctl.page);
  492. unlock_page(ctl.page);
  493. page_cache_release(ctl.page);
  494. }
  495. if (page) {
  496. cache->head = ctl.head;
  497. kunmap(page);
  498. SetPageUptodate(page);
  499. unlock_page(page);
  500. page_cache_release(page);
  501. }
  502. out:
  503. return result;
  504. }
  505. static int
  506. ncp_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  507. struct ncp_cache_control *ctrl, struct ncp_entry_info *entry,
  508. int inval_childs)
  509. {
  510. struct dentry *newdent, *dentry = filp->f_path.dentry;
  511. struct inode *dir = dentry->d_inode;
  512. struct ncp_cache_control ctl = *ctrl;
  513. struct qstr qname;
  514. int valid = 0;
  515. int hashed = 0;
  516. ino_t ino = 0;
  517. __u8 __name[NCP_MAXPATHLEN + 1];
  518. qname.len = sizeof(__name);
  519. if (ncp_vol2io(NCP_SERVER(dir), __name, &qname.len,
  520. entry->i.entryName, entry->i.nameLen,
  521. !ncp_preserve_entry_case(dir, entry->i.NSCreator)))
  522. return 1; /* I'm not sure */
  523. qname.name = __name;
  524. qname.hash = full_name_hash(qname.name, qname.len);
  525. if (dentry->d_op && dentry->d_op->d_hash)
  526. if (dentry->d_op->d_hash(dentry, dentry->d_inode, &qname) != 0)
  527. goto end_advance;
  528. newdent = d_lookup(dentry, &qname);
  529. if (!newdent) {
  530. newdent = d_alloc(dentry, &qname);
  531. if (!newdent)
  532. goto end_advance;
  533. } else {
  534. hashed = 1;
  535. /* If case sensitivity changed for this volume, all entries below this one
  536. should be thrown away. This entry itself is not affected, as its case
  537. sensitivity is controlled by its own parent. */
  538. if (inval_childs)
  539. shrink_dcache_parent(newdent);
  540. /*
  541. * NetWare's OS2 namespace is case preserving yet case
  542. * insensitive. So we update dentry's name as received from
  543. * server. Parent dir's i_mutex is locked because we're in
  544. * readdir.
  545. */
  546. dentry_update_name_case(newdent, &qname);
  547. }
  548. if (!newdent->d_inode) {
  549. struct inode *inode;
  550. entry->opened = 0;
  551. entry->ino = iunique(dir->i_sb, 2);
  552. inode = ncp_iget(dir->i_sb, entry);
  553. if (inode) {
  554. d_set_d_op(newdent, &ncp_dentry_operations);
  555. d_instantiate(newdent, inode);
  556. if (!hashed)
  557. d_rehash(newdent);
  558. }
  559. } else {
  560. struct inode *inode = newdent->d_inode;
  561. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  562. ncp_update_inode2(inode, entry);
  563. mutex_unlock(&inode->i_mutex);
  564. }
  565. if (newdent->d_inode) {
  566. ino = newdent->d_inode->i_ino;
  567. newdent->d_fsdata = (void *) ctl.fpos;
  568. ncp_new_dentry(newdent);
  569. }
  570. if (ctl.idx >= NCP_DIRCACHE_SIZE) {
  571. if (ctl.page) {
  572. kunmap(ctl.page);
  573. SetPageUptodate(ctl.page);
  574. unlock_page(ctl.page);
  575. page_cache_release(ctl.page);
  576. }
  577. ctl.cache = NULL;
  578. ctl.idx -= NCP_DIRCACHE_SIZE;
  579. ctl.ofs += 1;
  580. ctl.page = grab_cache_page(&dir->i_data, ctl.ofs);
  581. if (ctl.page)
  582. ctl.cache = kmap(ctl.page);
  583. }
  584. if (ctl.cache) {
  585. ctl.cache->dentry[ctl.idx] = newdent;
  586. valid = 1;
  587. }
  588. dput(newdent);
  589. end_advance:
  590. if (!valid)
  591. ctl.valid = 0;
  592. if (!ctl.filled && (ctl.fpos == filp->f_pos)) {
  593. if (!ino)
  594. ino = find_inode_number(dentry, &qname);
  595. if (!ino)
  596. ino = iunique(dir->i_sb, 2);
  597. ctl.filled = filldir(dirent, qname.name, qname.len,
  598. filp->f_pos, ino, DT_UNKNOWN);
  599. if (!ctl.filled)
  600. filp->f_pos += 1;
  601. }
  602. ctl.fpos += 1;
  603. ctl.idx += 1;
  604. *ctrl = ctl;
  605. return (ctl.valid || !ctl.filled);
  606. }
  607. static void
  608. ncp_read_volume_list(struct file *filp, void *dirent, filldir_t filldir,
  609. struct ncp_cache_control *ctl)
  610. {
  611. struct dentry *dentry = filp->f_path.dentry;
  612. struct inode *inode = dentry->d_inode;
  613. struct ncp_server *server = NCP_SERVER(inode);
  614. struct ncp_volume_info info;
  615. struct ncp_entry_info entry;
  616. int i;
  617. DPRINTK("ncp_read_volume_list: pos=%ld\n",
  618. (unsigned long) filp->f_pos);
  619. for (i = 0; i < NCP_NUMBER_OF_VOLUMES; i++) {
  620. int inval_dentry;
  621. if (ncp_get_volume_info_with_number(server, i, &info) != 0)
  622. return;
  623. if (!strlen(info.volume_name))
  624. continue;
  625. DPRINTK("ncp_read_volume_list: found vol: %s\n",
  626. info.volume_name);
  627. if (ncp_lookup_volume(server, info.volume_name,
  628. &entry.i)) {
  629. DPRINTK("ncpfs: could not lookup vol %s\n",
  630. info.volume_name);
  631. continue;
  632. }
  633. inval_dentry = ncp_update_known_namespace(server, entry.i.volNumber, NULL);
  634. entry.volume = entry.i.volNumber;
  635. if (!ncp_fill_cache(filp, dirent, filldir, ctl, &entry, inval_dentry))
  636. return;
  637. }
  638. }
  639. static void
  640. ncp_do_readdir(struct file *filp, void *dirent, filldir_t filldir,
  641. struct ncp_cache_control *ctl)
  642. {
  643. struct dentry *dentry = filp->f_path.dentry;
  644. struct inode *dir = dentry->d_inode;
  645. struct ncp_server *server = NCP_SERVER(dir);
  646. struct nw_search_sequence seq;
  647. struct ncp_entry_info entry;
  648. int err;
  649. void* buf;
  650. int more;
  651. size_t bufsize;
  652. DPRINTK("ncp_do_readdir: %s/%s, fpos=%ld\n",
  653. dentry->d_parent->d_name.name, dentry->d_name.name,
  654. (unsigned long) filp->f_pos);
  655. PPRINTK("ncp_do_readdir: init %s, volnum=%d, dirent=%u\n",
  656. dentry->d_name.name, NCP_FINFO(dir)->volNumber,
  657. NCP_FINFO(dir)->dirEntNum);
  658. err = ncp_initialize_search(server, dir, &seq);
  659. if (err) {
  660. DPRINTK("ncp_do_readdir: init failed, err=%d\n", err);
  661. return;
  662. }
  663. /* We MUST NOT use server->buffer_size handshaked with server if we are
  664. using UDP, as for UDP server uses max. buffer size determined by
  665. MTU, and for TCP server uses hardwired value 65KB (== 66560 bytes).
  666. So we use 128KB, just to be sure, as there is no way how to know
  667. this value in advance. */
  668. bufsize = 131072;
  669. buf = vmalloc(bufsize);
  670. if (!buf)
  671. return;
  672. do {
  673. int cnt;
  674. char* rpl;
  675. size_t rpls;
  676. err = ncp_search_for_fileset(server, &seq, &more, &cnt, buf, bufsize, &rpl, &rpls);
  677. if (err) /* Error */
  678. break;
  679. if (!cnt) /* prevent endless loop */
  680. break;
  681. while (cnt--) {
  682. size_t onerpl;
  683. if (rpls < offsetof(struct nw_info_struct, entryName))
  684. break; /* short packet */
  685. ncp_extract_file_info(rpl, &entry.i);
  686. onerpl = offsetof(struct nw_info_struct, entryName) + entry.i.nameLen;
  687. if (rpls < onerpl)
  688. break; /* short packet */
  689. (void)ncp_obtain_nfs_info(server, &entry.i);
  690. rpl += onerpl;
  691. rpls -= onerpl;
  692. entry.volume = entry.i.volNumber;
  693. if (!ncp_fill_cache(filp, dirent, filldir, ctl, &entry, 0))
  694. break;
  695. }
  696. } while (more);
  697. vfree(buf);
  698. return;
  699. }
  700. int ncp_conn_logged_in(struct super_block *sb)
  701. {
  702. struct ncp_server* server = NCP_SBP(sb);
  703. int result;
  704. if (ncp_single_volume(server)) {
  705. int len;
  706. struct dentry* dent;
  707. __u32 volNumber;
  708. __le32 dirEntNum;
  709. __le32 DosDirNum;
  710. __u8 __name[NCP_MAXPATHLEN + 1];
  711. len = sizeof(__name);
  712. result = ncp_io2vol(server, __name, &len, server->m.mounted_vol,
  713. strlen(server->m.mounted_vol), 1);
  714. if (result)
  715. goto out;
  716. result = -ENOENT;
  717. if (ncp_get_volume_root(server, __name, &volNumber, &dirEntNum, &DosDirNum)) {
  718. PPRINTK("ncp_conn_logged_in: %s not found\n",
  719. server->m.mounted_vol);
  720. goto out;
  721. }
  722. dent = sb->s_root;
  723. if (dent) {
  724. struct inode* ino = dent->d_inode;
  725. if (ino) {
  726. ncp_update_known_namespace(server, volNumber, NULL);
  727. NCP_FINFO(ino)->volNumber = volNumber;
  728. NCP_FINFO(ino)->dirEntNum = dirEntNum;
  729. NCP_FINFO(ino)->DosDirNum = DosDirNum;
  730. result = 0;
  731. } else {
  732. DPRINTK("ncpfs: sb->s_root->d_inode == NULL!\n");
  733. }
  734. } else {
  735. DPRINTK("ncpfs: sb->s_root == NULL!\n");
  736. }
  737. } else
  738. result = 0;
  739. out:
  740. return result;
  741. }
  742. static struct dentry *ncp_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  743. {
  744. struct ncp_server *server = NCP_SERVER(dir);
  745. struct inode *inode = NULL;
  746. struct ncp_entry_info finfo;
  747. int error, res, len;
  748. __u8 __name[NCP_MAXPATHLEN + 1];
  749. error = -EIO;
  750. if (!ncp_conn_valid(server))
  751. goto finished;
  752. PPRINTK("ncp_lookup: server lookup for %s/%s\n",
  753. dentry->d_parent->d_name.name, dentry->d_name.name);
  754. len = sizeof(__name);
  755. if (ncp_is_server_root(dir)) {
  756. res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
  757. dentry->d_name.len, 1);
  758. if (!res)
  759. res = ncp_lookup_volume(server, __name, &(finfo.i));
  760. if (!res)
  761. ncp_update_known_namespace(server, finfo.i.volNumber, NULL);
  762. } else {
  763. res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
  764. dentry->d_name.len, !ncp_preserve_case(dir));
  765. if (!res)
  766. res = ncp_obtain_info(server, dir, __name, &(finfo.i));
  767. }
  768. PPRINTK("ncp_lookup: looked for %s/%s, res=%d\n",
  769. dentry->d_parent->d_name.name, __name, res);
  770. /*
  771. * If we didn't find an entry, make a negative dentry.
  772. */
  773. if (res)
  774. goto add_entry;
  775. /*
  776. * Create an inode for the entry.
  777. */
  778. finfo.opened = 0;
  779. finfo.ino = iunique(dir->i_sb, 2);
  780. finfo.volume = finfo.i.volNumber;
  781. error = -EACCES;
  782. inode = ncp_iget(dir->i_sb, &finfo);
  783. if (inode) {
  784. ncp_new_dentry(dentry);
  785. add_entry:
  786. d_set_d_op(dentry, &ncp_dentry_operations);
  787. d_add(dentry, inode);
  788. error = 0;
  789. }
  790. finished:
  791. PPRINTK("ncp_lookup: result=%d\n", error);
  792. return ERR_PTR(error);
  793. }
  794. /*
  795. * This code is common to create, mkdir, and mknod.
  796. */
  797. static int ncp_instantiate(struct inode *dir, struct dentry *dentry,
  798. struct ncp_entry_info *finfo)
  799. {
  800. struct inode *inode;
  801. int error = -EINVAL;
  802. finfo->ino = iunique(dir->i_sb, 2);
  803. inode = ncp_iget(dir->i_sb, finfo);
  804. if (!inode)
  805. goto out_close;
  806. d_instantiate(dentry,inode);
  807. error = 0;
  808. out:
  809. return error;
  810. out_close:
  811. PPRINTK("ncp_instantiate: %s/%s failed, closing file\n",
  812. dentry->d_parent->d_name.name, dentry->d_name.name);
  813. ncp_close_file(NCP_SERVER(dir), finfo->file_handle);
  814. goto out;
  815. }
  816. int ncp_create_new(struct inode *dir, struct dentry *dentry, int mode,
  817. dev_t rdev, __le32 attributes)
  818. {
  819. struct ncp_server *server = NCP_SERVER(dir);
  820. struct ncp_entry_info finfo;
  821. int error, result, len;
  822. int opmode;
  823. __u8 __name[NCP_MAXPATHLEN + 1];
  824. PPRINTK("ncp_create_new: creating %s/%s, mode=%x\n",
  825. dentry->d_parent->d_name.name, dentry->d_name.name, mode);
  826. ncp_age_dentry(server, dentry);
  827. len = sizeof(__name);
  828. error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
  829. dentry->d_name.len, !ncp_preserve_case(dir));
  830. if (error)
  831. goto out;
  832. error = -EACCES;
  833. if (S_ISREG(mode) &&
  834. (server->m.flags & NCP_MOUNT_EXTRAS) &&
  835. (mode & S_IXUGO))
  836. attributes |= aSYSTEM | aSHARED;
  837. result = ncp_open_create_file_or_subdir(server, dir, __name,
  838. OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
  839. attributes, AR_READ | AR_WRITE, &finfo);
  840. opmode = O_RDWR;
  841. if (result) {
  842. result = ncp_open_create_file_or_subdir(server, dir, __name,
  843. OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
  844. attributes, AR_WRITE, &finfo);
  845. if (result) {
  846. if (result == 0x87)
  847. error = -ENAMETOOLONG;
  848. else if (result < 0)
  849. error = result;
  850. DPRINTK("ncp_create: %s/%s failed\n",
  851. dentry->d_parent->d_name.name, dentry->d_name.name);
  852. goto out;
  853. }
  854. opmode = O_WRONLY;
  855. }
  856. finfo.access = opmode;
  857. if (ncp_is_nfs_extras(server, finfo.volume)) {
  858. finfo.i.nfs.mode = mode;
  859. finfo.i.nfs.rdev = new_encode_dev(rdev);
  860. if (ncp_modify_nfs_info(server, finfo.volume,
  861. finfo.i.dirEntNum,
  862. mode, new_encode_dev(rdev)) != 0)
  863. goto out;
  864. }
  865. error = ncp_instantiate(dir, dentry, &finfo);
  866. out:
  867. return error;
  868. }
  869. static int ncp_create(struct inode *dir, struct dentry *dentry, int mode,
  870. struct nameidata *nd)
  871. {
  872. return ncp_create_new(dir, dentry, mode, 0, 0);
  873. }
  874. static int ncp_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  875. {
  876. struct ncp_entry_info finfo;
  877. struct ncp_server *server = NCP_SERVER(dir);
  878. int error, len;
  879. __u8 __name[NCP_MAXPATHLEN + 1];
  880. DPRINTK("ncp_mkdir: making %s/%s\n",
  881. dentry->d_parent->d_name.name, dentry->d_name.name);
  882. ncp_age_dentry(server, dentry);
  883. len = sizeof(__name);
  884. error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
  885. dentry->d_name.len, !ncp_preserve_case(dir));
  886. if (error)
  887. goto out;
  888. error = ncp_open_create_file_or_subdir(server, dir, __name,
  889. OC_MODE_CREATE, aDIR,
  890. cpu_to_le16(0xffff),
  891. &finfo);
  892. if (error == 0) {
  893. if (ncp_is_nfs_extras(server, finfo.volume)) {
  894. mode |= S_IFDIR;
  895. finfo.i.nfs.mode = mode;
  896. if (ncp_modify_nfs_info(server,
  897. finfo.volume,
  898. finfo.i.dirEntNum,
  899. mode, 0) != 0)
  900. goto out;
  901. }
  902. error = ncp_instantiate(dir, dentry, &finfo);
  903. } else if (error > 0) {
  904. error = -EACCES;
  905. }
  906. out:
  907. return error;
  908. }
  909. static int ncp_rmdir(struct inode *dir, struct dentry *dentry)
  910. {
  911. struct ncp_server *server = NCP_SERVER(dir);
  912. int error, result, len;
  913. __u8 __name[NCP_MAXPATHLEN + 1];
  914. DPRINTK("ncp_rmdir: removing %s/%s\n",
  915. dentry->d_parent->d_name.name, dentry->d_name.name);
  916. error = -EBUSY;
  917. if (!d_unhashed(dentry))
  918. goto out;
  919. len = sizeof(__name);
  920. error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
  921. dentry->d_name.len, !ncp_preserve_case(dir));
  922. if (error)
  923. goto out;
  924. result = ncp_del_file_or_subdir(server, dir, __name);
  925. switch (result) {
  926. case 0x00:
  927. error = 0;
  928. break;
  929. case 0x85: /* unauthorized to delete file */
  930. case 0x8A: /* unauthorized to delete file */
  931. error = -EACCES;
  932. break;
  933. case 0x8F:
  934. case 0x90: /* read only */
  935. error = -EPERM;
  936. break;
  937. case 0x9F: /* in use by another client */
  938. error = -EBUSY;
  939. break;
  940. case 0xA0: /* directory not empty */
  941. error = -ENOTEMPTY;
  942. break;
  943. case 0xFF: /* someone deleted file */
  944. error = -ENOENT;
  945. break;
  946. default:
  947. error = result < 0 ? result : -EACCES;
  948. break;
  949. }
  950. out:
  951. return error;
  952. }
  953. static int ncp_unlink(struct inode *dir, struct dentry *dentry)
  954. {
  955. struct inode *inode = dentry->d_inode;
  956. struct ncp_server *server;
  957. int error;
  958. server = NCP_SERVER(dir);
  959. DPRINTK("ncp_unlink: unlinking %s/%s\n",
  960. dentry->d_parent->d_name.name, dentry->d_name.name);
  961. /*
  962. * Check whether to close the file ...
  963. */
  964. if (inode) {
  965. PPRINTK("ncp_unlink: closing file\n");
  966. ncp_make_closed(inode);
  967. }
  968. error = ncp_del_file_or_subdir2(server, dentry);
  969. #ifdef CONFIG_NCPFS_STRONG
  970. /* 9C is Invalid path.. It should be 8F, 90 - read only, but
  971. it is not :-( */
  972. if ((error == 0x9C || error == 0x90) && server->m.flags & NCP_MOUNT_STRONG) { /* R/O */
  973. error = ncp_force_unlink(dir, dentry);
  974. }
  975. #endif
  976. switch (error) {
  977. case 0x00:
  978. DPRINTK("ncp: removed %s/%s\n",
  979. dentry->d_parent->d_name.name, dentry->d_name.name);
  980. break;
  981. case 0x85:
  982. case 0x8A:
  983. error = -EACCES;
  984. break;
  985. case 0x8D: /* some files in use */
  986. case 0x8E: /* all files in use */
  987. error = -EBUSY;
  988. break;
  989. case 0x8F: /* some read only */
  990. case 0x90: /* all read only */
  991. case 0x9C: /* !!! returned when in-use or read-only by NW4 */
  992. error = -EPERM;
  993. break;
  994. case 0xFF:
  995. error = -ENOENT;
  996. break;
  997. default:
  998. error = error < 0 ? error : -EACCES;
  999. break;
  1000. }
  1001. return error;
  1002. }
  1003. static int ncp_rename(struct inode *old_dir, struct dentry *old_dentry,
  1004. struct inode *new_dir, struct dentry *new_dentry)
  1005. {
  1006. struct ncp_server *server = NCP_SERVER(old_dir);
  1007. int error;
  1008. int old_len, new_len;
  1009. __u8 __old_name[NCP_MAXPATHLEN + 1], __new_name[NCP_MAXPATHLEN + 1];
  1010. DPRINTK("ncp_rename: %s/%s to %s/%s\n",
  1011. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1012. new_dentry->d_parent->d_name.name, new_dentry->d_name.name);
  1013. ncp_age_dentry(server, old_dentry);
  1014. ncp_age_dentry(server, new_dentry);
  1015. old_len = sizeof(__old_name);
  1016. error = ncp_io2vol(server, __old_name, &old_len,
  1017. old_dentry->d_name.name, old_dentry->d_name.len,
  1018. !ncp_preserve_case(old_dir));
  1019. if (error)
  1020. goto out;
  1021. new_len = sizeof(__new_name);
  1022. error = ncp_io2vol(server, __new_name, &new_len,
  1023. new_dentry->d_name.name, new_dentry->d_name.len,
  1024. !ncp_preserve_case(new_dir));
  1025. if (error)
  1026. goto out;
  1027. error = ncp_ren_or_mov_file_or_subdir(server, old_dir, __old_name,
  1028. new_dir, __new_name);
  1029. #ifdef CONFIG_NCPFS_STRONG
  1030. if ((error == 0x90 || error == 0x8B || error == -EACCES) &&
  1031. server->m.flags & NCP_MOUNT_STRONG) { /* RO */
  1032. error = ncp_force_rename(old_dir, old_dentry, __old_name,
  1033. new_dir, new_dentry, __new_name);
  1034. }
  1035. #endif
  1036. switch (error) {
  1037. case 0x00:
  1038. DPRINTK("ncp renamed %s -> %s.\n",
  1039. old_dentry->d_name.name,new_dentry->d_name.name);
  1040. break;
  1041. case 0x9E:
  1042. error = -ENAMETOOLONG;
  1043. break;
  1044. case 0xFF:
  1045. error = -ENOENT;
  1046. break;
  1047. default:
  1048. error = error < 0 ? error : -EACCES;
  1049. break;
  1050. }
  1051. out:
  1052. return error;
  1053. }
  1054. static int ncp_mknod(struct inode * dir, struct dentry *dentry,
  1055. int mode, dev_t rdev)
  1056. {
  1057. if (!new_valid_dev(rdev))
  1058. return -EINVAL;
  1059. if (ncp_is_nfs_extras(NCP_SERVER(dir), NCP_FINFO(dir)->volNumber)) {
  1060. DPRINTK(KERN_DEBUG "ncp_mknod: mode = 0%o\n", mode);
  1061. return ncp_create_new(dir, dentry, mode, rdev, 0);
  1062. }
  1063. return -EPERM; /* Strange, but true */
  1064. }
  1065. /* The following routines are taken directly from msdos-fs */
  1066. /* Linear day numbers of the respective 1sts in non-leap years. */
  1067. static int day_n[] =
  1068. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
  1069. /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
  1070. extern struct timezone sys_tz;
  1071. static int utc2local(int time)
  1072. {
  1073. return time - sys_tz.tz_minuteswest * 60;
  1074. }
  1075. static int local2utc(int time)
  1076. {
  1077. return time + sys_tz.tz_minuteswest * 60;
  1078. }
  1079. /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
  1080. int
  1081. ncp_date_dos2unix(__le16 t, __le16 d)
  1082. {
  1083. unsigned short time = le16_to_cpu(t), date = le16_to_cpu(d);
  1084. int month, year, secs;
  1085. /* first subtract and mask after that... Otherwise, if
  1086. date == 0, bad things happen */
  1087. month = ((date >> 5) - 1) & 15;
  1088. year = date >> 9;
  1089. secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 +
  1090. 86400 * ((date & 31) - 1 + day_n[month] + (year / 4) +
  1091. year * 365 - ((year & 3) == 0 && month < 2 ? 1 : 0) + 3653);
  1092. /* days since 1.1.70 plus 80's leap day */
  1093. return local2utc(secs);
  1094. }
  1095. /* Convert linear UNIX date to a MS-DOS time/date pair. */
  1096. void
  1097. ncp_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
  1098. {
  1099. int day, year, nl_day, month;
  1100. unix_date = utc2local(unix_date);
  1101. *time = cpu_to_le16(
  1102. (unix_date % 60) / 2 + (((unix_date / 60) % 60) << 5) +
  1103. (((unix_date / 3600) % 24) << 11));
  1104. day = unix_date / 86400 - 3652;
  1105. year = day / 365;
  1106. if ((year + 3) / 4 + 365 * year > day)
  1107. year--;
  1108. day -= (year + 3) / 4 + 365 * year;
  1109. if (day == 59 && !(year & 3)) {
  1110. nl_day = day;
  1111. month = 2;
  1112. } else {
  1113. nl_day = (year & 3) || day <= 59 ? day : day - 1;
  1114. for (month = 1; month < 12; month++)
  1115. if (day_n[month] > nl_day)
  1116. break;
  1117. }
  1118. *date = cpu_to_le16(nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9));
  1119. }