dir.c 32 KB

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