original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1093 lines
26 KiB

[PATCH] VFS: Permit filesystem to override root dentry on mount Extend the get_sb() filesystem operation to take an extra argument that permits the VFS to pass in the target vfsmount that defines the mountpoint. The filesystem is then required to manually set the superblock and root dentry pointers. For most filesystems, this should be done with simple_set_mnt() which will set the superblock pointer and then set the root dentry to the superblock's s_root (as per the old default behaviour). The get_sb() op now returns an integer as there's now no need to return the superblock pointer. This patch permits a superblock to be implicitly shared amongst several mount points, such as can be done with NFS to avoid potential inode aliasing. In such a case, simple_set_mnt() would not be called, and instead the mnt_root and mnt_sb would be set directly. The patch also makes the following changes: (*) the get_sb_*() convenience functions in the core kernel now take a vfsmount pointer argument and return an integer, so most filesystems have to change very little. (*) If one of the convenience function is not used, then get_sb() should normally call simple_set_mnt() to instantiate the vfsmount. This will always return 0, and so can be tail-called from get_sb(). (*) generic_shutdown_super() now calls shrink_dcache_sb() to clean up the dcache upon superblock destruction rather than shrink_dcache_anon(). This is required because the superblock may now have multiple trees that aren't actually bound to s_root, but that still need to be cleaned up. The currently called functions assume that the whole tree is rooted at s_root, and that anonymous dentries are not the roots of trees which results in dentries being left unculled. However, with the way NFS superblock sharing are currently set to be implemented, these assumptions are violated: the root of the filesystem is simply a dummy dentry and inode (the real inode for '/' may well be inaccessible), and all the vfsmounts are rooted on anonymous[*] dentries with child trees. [*] Anonymous until discovered from another tree. (*) The documentation has been adjusted, including the additional bit of changing ext2_* into foo_* in the documentation. [akpm@osdl.org: convert ipath_fs, do other stuff] Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: Al Viro <viro@zeniv.linux.org.uk> Cc: Nathan Scott <nathans@sgi.com> Cc: Roland Dreier <rolandd@cisco.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
16 years ago
fs: Limit sys_mount to only request filesystem modules. Modify the request_module to prefix the file system type with "fs-" and add aliases to all of the filesystems that can be built as modules to match. A common practice is to build all of the kernel code and leave code that is not commonly needed as modules, with the result that many users are exposed to any bug anywhere in the kernel. Looking for filesystems with a fs- prefix limits the pool of possible modules that can be loaded by mount to just filesystems trivially making things safer with no real cost. Using aliases means user space can control the policy of which filesystem modules are auto-loaded by editing /etc/modprobe.d/*.conf with blacklist and alias directives. Allowing simple, safe, well understood work-arounds to known problematic software. This also addresses a rare but unfortunate problem where the filesystem name is not the same as it's module name and module auto-loading would not work. While writing this patch I saw a handful of such cases. The most significant being autofs that lives in the module autofs4. This is relevant to user namespaces because we can reach the request module in get_fs_type() without having any special permissions, and people get uncomfortable when a user specified string (in this case the filesystem type) goes all of the way to request_module. After having looked at this issue I don't think there is any particular reason to perform any filtering or permission checks beyond making it clear in the module request that we want a filesystem module. The common pattern in the kernel is to call request_module() without regards to the users permissions. In general all a filesystem module does once loaded is call register_filesystem() and go to sleep. Which means there is not much attack surface exposed by loading a filesytem module unless the filesystem is mounted. In a user namespace filesystems are not mounted unless .fs_flags = FS_USERNS_MOUNT, which most filesystems do not set today. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Acked-by: Kees Cook <keescook@chromium.org> Reported-by: Kees Cook <keescook@google.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
9 years ago
  1. /*
  2. * linux/fs/vfat/namei.c
  3. *
  4. * Written 1992,1993 by Werner Almesberger
  5. *
  6. * Windows95/Windows NT compatible extended MSDOS filesystem
  7. * by Gordon Chaffee Copyright (C) 1995. Send bug reports for the
  8. * VFAT filesystem to <chaffee@cs.berkeley.edu>. Specify
  9. * what file operation caused you trouble and if you can duplicate
  10. * the problem, send a script that demonstrates it.
  11. *
  12. * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
  13. *
  14. * Support Multibyte characters and cleanup by
  15. * OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
  16. */
  17. #include <linux/module.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/ctype.h>
  20. #include <linux/slab.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/namei.h>
  23. #include "fat.h"
  24. /*
  25. * If new entry was created in the parent, it could create the 8.3
  26. * alias (the shortname of logname). So, the parent may have the
  27. * negative-dentry which matches the created 8.3 alias.
  28. *
  29. * If it happened, the negative dentry isn't actually negative
  30. * anymore. So, drop it.
  31. */
  32. static int vfat_revalidate_shortname(struct dentry *dentry)
  33. {
  34. int ret = 1;
  35. spin_lock(&dentry->d_lock);
  36. if (dentry->d_time != dentry->d_parent->d_inode->i_version)
  37. ret = 0;
  38. spin_unlock(&dentry->d_lock);
  39. return ret;
  40. }
  41. static int vfat_revalidate(struct dentry *dentry, unsigned int flags)
  42. {
  43. if (flags & LOOKUP_RCU)
  44. return -ECHILD;
  45. /* This is not negative dentry. Always valid. */
  46. if (dentry->d_inode)
  47. return 1;
  48. return vfat_revalidate_shortname(dentry);
  49. }
  50. static int vfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
  51. {
  52. if (flags & LOOKUP_RCU)
  53. return -ECHILD;
  54. /*
  55. * This is not negative dentry. Always valid.
  56. *
  57. * Note, rename() to existing directory entry will have ->d_inode,
  58. * and will use existing name which isn't specified name by user.
  59. *
  60. * We may be able to drop this positive dentry here. But dropping
  61. * positive dentry isn't good idea. So it's unsupported like
  62. * rename("filename", "FILENAME") for now.
  63. */
  64. if (dentry->d_inode)
  65. return 1;
  66. /*
  67. * This may be nfsd (or something), anyway, we can't see the
  68. * intent of this. So, since this can be for creation, drop it.
  69. */
  70. if (!flags)
  71. return 0;
  72. /*
  73. * Drop the negative dentry, in order to make sure to use the
  74. * case sensitive name which is specified by user if this is
  75. * for creation.
  76. */
  77. if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  78. return 0;
  79. return vfat_revalidate_shortname(dentry);
  80. }
  81. /* returns the length of a struct qstr, ignoring trailing dots */
  82. static unsigned int __vfat_striptail_len(unsigned int len, const char *name)
  83. {
  84. while (len && name[len - 1] == '.')
  85. len--;
  86. return len;
  87. }
  88. static unsigned int vfat_striptail_len(const struct qstr *qstr)
  89. {
  90. return __vfat_striptail_len(qstr->len, qstr->name);
  91. }
  92. /*
  93. * Compute the hash for the vfat name corresponding to the dentry.
  94. * Note: if the name is invalid, we leave the hash code unchanged so
  95. * that the existing dentry can be used. The vfat fs routines will
  96. * return ENOENT or EINVAL as appropriate.
  97. */
  98. static int vfat_hash(const struct dentry *dentry, const struct inode *inode,
  99. struct qstr *qstr)
  100. {
  101. qstr->hash = full_name_hash(qstr->name, vfat_striptail_len(qstr));
  102. return 0;
  103. }
  104. /*
  105. * Compute the hash for the vfat name corresponding to the dentry.
  106. * Note: if the name is invalid, we leave the hash code unchanged so
  107. * that the existing dentry can be used. The vfat fs routines will
  108. * return ENOENT or EINVAL as appropriate.
  109. */
  110. static int vfat_hashi(const struct dentry *dentry, const struct inode *inode,
  111. struct qstr *qstr)
  112. {
  113. struct nls_table *t = MSDOS_SB(dentry->d_sb)->nls_io;
  114. const unsigned char *name;
  115. unsigned int len;
  116. unsigned long hash;
  117. name = qstr->name;
  118. len = vfat_striptail_len(qstr);
  119. hash = init_name_hash();
  120. while (len--)
  121. hash = partial_name_hash(nls_tolower(t, *name++), hash);
  122. qstr->hash = end_name_hash(hash);
  123. return 0;
  124. }
  125. /*
  126. * Case insensitive compare of two vfat names.
  127. */
  128. static int vfat_cmpi(const struct dentry *parent, const struct inode *pinode,
  129. const struct dentry *dentry, const struct inode *inode,
  130. unsigned int len, const char *str, const struct qstr *name)
  131. {
  132. struct nls_table *t = MSDOS_SB(parent->d_sb)->nls_io;
  133. unsigned int alen, blen;
  134. /* A filename cannot end in '.' or we treat it like it has none */
  135. alen = vfat_striptail_len(name);
  136. blen = __vfat_striptail_len(len, str);
  137. if (alen == blen) {
  138. if (nls_strnicmp(t, name->name, str, alen) == 0)
  139. return 0;
  140. }
  141. return 1;
  142. }
  143. /*
  144. * Case sensitive compare of two vfat names.
  145. */
  146. static int vfat_cmp(const struct dentry *parent, const struct inode *pinode,
  147. const struct dentry *dentry, const struct inode *inode,
  148. unsigned int len, const char *str, const struct qstr *name)
  149. {
  150. unsigned int alen, blen;
  151. /* A filename cannot end in '.' or we treat it like it has none */
  152. alen = vfat_striptail_len(name);
  153. blen = __vfat_striptail_len(len, str);
  154. if (alen == blen) {
  155. if (strncmp(name->name, str, alen) == 0)
  156. return 0;
  157. }
  158. return 1;
  159. }
  160. static const struct dentry_operations vfat_ci_dentry_ops = {
  161. .d_revalidate = vfat_revalidate_ci,
  162. .d_hash = vfat_hashi,
  163. .d_compare = vfat_cmpi,
  164. };
  165. static const struct dentry_operations vfat_dentry_ops = {
  166. .d_revalidate = vfat_revalidate,
  167. .d_hash = vfat_hash,
  168. .d_compare = vfat_cmp,
  169. };
  170. /* Characters that are undesirable in an MS-DOS file name */
  171. static inline wchar_t vfat_bad_char(wchar_t w)
  172. {
  173. return (w < 0x0020)
  174. || (w == '*') || (w == '?') || (w == '<') || (w == '>')
  175. || (w == '|') || (w == '"') || (w == ':') || (w == '/')
  176. || (w == '\\');
  177. }
  178. static inline wchar_t vfat_replace_char(wchar_t w)
  179. {
  180. return (w == '[') || (w == ']') || (w == ';') || (w == ',')
  181. || (w == '+') || (w == '=');
  182. }
  183. static wchar_t vfat_skip_char(wchar_t w)
  184. {
  185. return (w == '.') || (w == ' ');
  186. }
  187. static inline int vfat_is_used_badchars(const wchar_t *s, int len)
  188. {
  189. int i;
  190. for (i = 0; i < len; i++)
  191. if (vfat_bad_char(s[i]))
  192. return -EINVAL;
  193. if (s[i - 1] == ' ') /* last character cannot be space */
  194. return -EINVAL;
  195. return 0;
  196. }
  197. static int vfat_find_form(struct inode *dir, unsigned char *name)
  198. {
  199. struct fat_slot_info sinfo;
  200. int err = fat_scan(dir, name, &sinfo);
  201. if (err)
  202. return -ENOENT;
  203. brelse(sinfo.bh);
  204. return 0;
  205. }
  206. /*
  207. * 1) Valid characters for the 8.3 format alias are any combination of
  208. * letters, uppercase alphabets, digits, any of the
  209. * following special characters:
  210. * $ % ' ` - @ { } ~ ! # ( ) & _ ^
  211. * In this case Longfilename is not stored in disk.
  212. *
  213. * WinNT's Extension:
  214. * File name and extension name is contain uppercase/lowercase
  215. * only. And it is expressed by CASE_LOWER_BASE and CASE_LOWER_EXT.
  216. *
  217. * 2) File name is 8.3 format, but it contain the uppercase and
  218. * lowercase char, muliti bytes char, etc. In this case numtail is not
  219. * added, but Longfilename is stored.
  220. *
  221. * 3) When the one except for the above, or the following special
  222. * character are contained:
  223. * . [ ] ; , + =
  224. * numtail is added, and Longfilename must be stored in disk .
  225. */
  226. struct shortname_info {
  227. unsigned char lower:1,
  228. upper:1,
  229. valid:1;
  230. };
  231. #define INIT_SHORTNAME_INFO(x) do { \
  232. (x)->lower = 1; \
  233. (x)->upper = 1; \
  234. (x)->valid = 1; \
  235. } while (0)
  236. static inline int to_shortname_char(struct nls_table *nls,
  237. unsigned char *buf, int buf_size,
  238. wchar_t *src, struct shortname_info *info)
  239. {
  240. int len;
  241. if (vfat_skip_char(*src)) {
  242. info->valid = 0;
  243. return 0;
  244. }
  245. if (vfat_replace_char(*src)) {
  246. info->valid = 0;
  247. buf[0] = '_';
  248. return 1;
  249. }
  250. len = nls->uni2char(*src, buf, buf_size);
  251. if (len <= 0) {
  252. info->valid = 0;
  253. buf[0] = '_';
  254. len = 1;
  255. } else if (len == 1) {
  256. unsigned char prev = buf[0];
  257. if (buf[0] >= 0x7F) {
  258. info->lower = 0;
  259. info->upper = 0;
  260. }
  261. buf[0] = nls_toupper(nls, buf[0]);
  262. if (isalpha(buf[0])) {
  263. if (buf[0] == prev)
  264. info->lower = 0;
  265. else
  266. info->upper = 0;
  267. }
  268. } else {
  269. info->lower = 0;
  270. info->upper = 0;
  271. }
  272. return len;
  273. }
  274. /*
  275. * Given a valid longname, create a unique shortname. Make sure the
  276. * shortname does not exist
  277. * Returns negative number on error, 0 for a normal
  278. * return, and 1 for valid shortname
  279. */
  280. static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
  281. wchar_t *uname, int ulen,
  282. unsigned char *name_res, unsigned char *lcase)
  283. {
  284. struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options;
  285. wchar_t *ip, *ext_start, *end, *name_start;
  286. unsigned char base[9], ext[4], buf[5], *p;
  287. unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
  288. int chl, chi;
  289. int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
  290. int is_shortname;
  291. struct shortname_info base_info, ext_info;
  292. is_shortname = 1;
  293. INIT_SHORTNAME_INFO(&base_info);
  294. INIT_SHORTNAME_INFO(&ext_info);
  295. /* Now, we need to create a shortname from the long name */
  296. ext_start = end = &uname[ulen];
  297. while (--ext_start >= uname) {
  298. if (*ext_start == 0x002E) { /* is `.' */
  299. if (ext_start == end - 1) {
  300. sz = ulen;
  301. ext_start = NULL;
  302. }
  303. break;
  304. }
  305. }
  306. if (ext_start == uname - 1) {
  307. sz = ulen;
  308. ext_start = NULL;
  309. } else if (ext_start) {
  310. /*
  311. * Names which start with a dot could be just
  312. * an extension eg. "...test". In this case Win95
  313. * uses the extension as the name and sets no extension.
  314. */
  315. name_start = &uname[0];
  316. while (name_start < ext_start) {
  317. if (!vfat_skip_char(*name_start))
  318. break;
  319. name_start++;
  320. }
  321. if (name_start != ext_start) {
  322. sz = ext_start - uname;
  323. ext_start++;
  324. } else {
  325. sz = ulen;
  326. ext_start = NULL;
  327. }
  328. }
  329. numtail_baselen = 6;
  330. numtail2_baselen = 2;
  331. for (baselen = i = 0, p = base, ip = uname; i < sz; i++, ip++) {
  332. chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
  333. ip, &base_info);
  334. if (chl == 0)
  335. continue;
  336. if (baselen < 2 && (baselen + chl) > 2)
  337. numtail2_baselen = baselen;
  338. if (baselen < 6 && (baselen + chl) > 6)
  339. numtail_baselen = baselen;
  340. for (chi = 0; chi < chl; chi++) {
  341. *p++ = charbuf[chi];
  342. baselen++;
  343. if (baselen >= 8)
  344. break;
  345. }
  346. if (baselen >= 8) {
  347. if ((chi < chl - 1) || (ip + 1) - uname < sz)
  348. is_shortname = 0;
  349. break;
  350. }
  351. }
  352. if (baselen == 0) {
  353. return -EINVAL;
  354. }
  355. extlen = 0;
  356. if (ext_start) {
  357. for (p = ext, ip = ext_start; extlen < 3 && ip < end; ip++) {
  358. chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
  359. ip, &ext_info);
  360. if (chl == 0)
  361. continue;
  362. if ((extlen + chl) > 3) {
  363. is_shortname = 0;
  364. break;
  365. }
  366. for (chi = 0; chi < chl; chi++) {
  367. *p++ = charbuf[chi];
  368. extlen++;
  369. }
  370. if (extlen >= 3) {
  371. if (ip + 1 != end)
  372. is_shortname = 0;
  373. break;
  374. }
  375. }
  376. }
  377. ext[extlen] = '\0';
  378. base[baselen] = '\0';
  379. /* Yes, it can happen. ".\xe5" would do it. */
  380. if (base[0] == DELETED_FLAG)
  381. base[0] = 0x05;
  382. /* OK, at this point we know that base is not longer than 8 symbols,
  383. * ext is not longer than 3, base is nonempty, both don't contain
  384. * any bad symbols (lowercase transformed to uppercase).
  385. */
  386. memset(name_res, ' ', MSDOS_NAME);
  387. memcpy(name_res, base, baselen);
  388. memcpy(name_res + 8, ext, extlen);
  389. *lcase = 0;
  390. if (is_shortname && base_info.valid && ext_info.valid) {
  391. if (vfat_find_form(dir, name_res) == 0)
  392. return -EEXIST;
  393. if (opts->shortname & VFAT_SFN_CREATE_WIN95) {
  394. return (base_info.upper && ext_info.upper);
  395. } else if (opts->shortname & VFAT_SFN_CREATE_WINNT) {
  396. if ((base_info.upper || base_info.lower) &&
  397. (ext_info.upper || ext_info.lower)) {
  398. if (!base_info.upper && base_info.lower)
  399. *lcase |= CASE_LOWER_BASE;
  400. if (!ext_info.upper && ext_info.lower)
  401. *lcase |= CASE_LOWER_EXT;
  402. return 1;
  403. }
  404. return 0;
  405. } else {
  406. BUG();
  407. }
  408. }
  409. if (opts->numtail == 0)
  410. if (vfat_find_form(dir, name_res) < 0)
  411. return 0;
  412. /*
  413. * Try to find a unique extension. This used to
  414. * iterate through all possibilities sequentially,
  415. * but that gave extremely bad performance. Windows
  416. * only tries a few cases before using random
  417. * values for part of the base.
  418. */
  419. if (baselen > 6) {
  420. baselen = numtail_baselen;
  421. name_res[7] = ' ';
  422. }
  423. name_res[baselen] = '~';
  424. for (i = 1; i < 10; i++) {
  425. name_res[baselen + 1] = i + '0';
  426. if (vfat_find_form(dir, name_res) < 0)
  427. return 0;
  428. }
  429. i = jiffies;
  430. sz = (jiffies >> 16) & 0x7;
  431. if (baselen > 2) {
  432. baselen = numtail2_baselen;
  433. name_res[7] = ' ';
  434. }
  435. name_res[baselen + 4] = '~';
  436. name_res[baselen + 5] = '1' + sz;
  437. while (1) {
  438. snprintf(buf, sizeof(buf), "%04X", i & 0xffff);
  439. memcpy(&name_res[baselen], buf, 4);
  440. if (vfat_find_form(dir, name_res) < 0)
  441. break;
  442. i -= 11;
  443. }
  444. return 0;
  445. }
  446. /* Translate a string, including coded sequences into Unicode */
  447. static int
  448. xlate_to_uni(const unsigned char *name, int len, unsigned char *outname,
  449. int *longlen, int *outlen, int escape, int utf8,
  450. struct nls_table *nls)
  451. {
  452. const unsigned char *ip;
  453. unsigned char nc;
  454. unsigned char *op;
  455. unsigned int ec;
  456. int i, k, fill;
  457. int charlen;
  458. if (utf8) {
  459. *outlen = utf8s_to_utf16s(name, len, UTF16_HOST_ENDIAN,
  460. (wchar_t *) outname, FAT_LFN_LEN + 2);
  461. if (*outlen < 0)
  462. return *outlen;
  463. else if (*outlen > FAT_LFN_LEN)
  464. return -ENAMETOOLONG;
  465. op = &outname[*outlen * sizeof(wchar_t)];
  466. } else {
  467. for (i = 0, ip = name, op = outname, *outlen = 0;
  468. i < len && *outlen < FAT_LFN_LEN;
  469. *outlen += 1) {
  470. if (escape && (*ip == ':')) {
  471. if (i > len - 5)
  472. return -EINVAL;
  473. ec = 0;
  474. for (k = 1; k < 5; k++) {
  475. nc = ip[k];
  476. ec <<= 4;
  477. if (nc >= '0' && nc <= '9') {
  478. ec |= nc - '0';
  479. continue;
  480. }
  481. if (nc >= 'a' && nc <= 'f') {
  482. ec |= nc - ('a' - 10);
  483. continue;
  484. }
  485. if (nc >= 'A' && nc <= 'F') {
  486. ec |= nc - ('A' - 10);
  487. continue;
  488. }
  489. return -EINVAL;
  490. }
  491. *op++ = ec & 0xFF;
  492. *op++ = ec >> 8;
  493. ip += 5;
  494. i += 5;
  495. } else {
  496. charlen = nls->char2uni(ip, len - i,
  497. (wchar_t *)op);
  498. if (charlen < 0)
  499. return -EINVAL;
  500. ip += charlen;
  501. i += charlen;
  502. op += 2;
  503. }
  504. }
  505. if (i < len)
  506. return -ENAMETOOLONG;
  507. }
  508. *longlen = *outlen;
  509. if (*outlen % 13) {
  510. *op++ = 0;
  511. *op++ = 0;
  512. *outlen += 1;
  513. if (*outlen % 13) {
  514. fill = 13 - (*outlen % 13);
  515. for (i = 0; i < fill; i++) {
  516. *op++ = 0xff;
  517. *op++ = 0xff;
  518. }
  519. *outlen += fill;
  520. }
  521. }
  522. return 0;
  523. }
  524. static int vfat_build_slots(struct inode *dir, const unsigned char *name,
  525. int len, int is_dir, int cluster,
  526. struct timespec *ts,
  527. struct msdos_dir_slot *slots, int *nr_slots)
  528. {
  529. struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
  530. struct fat_mount_options *opts = &sbi->options;
  531. struct msdos_dir_slot *ps;
  532. struct msdos_dir_entry *de;
  533. unsigned char cksum, lcase;
  534. unsigned char msdos_name[MSDOS_NAME];
  535. wchar_t *uname;
  536. __le16 time, date;
  537. u8 time_cs;
  538. int err, ulen, usize, i;
  539. loff_t offset;
  540. *nr_slots = 0;
  541. uname = __getname();
  542. if (!uname)
  543. return -ENOMEM;
  544. err = xlate_to_uni(name, len, (unsigned char *)uname, &ulen, &usize,
  545. opts->unicode_xlate, opts->utf8, sbi->nls_io);
  546. if (err)
  547. goto out_free;
  548. err = vfat_is_used_badchars(uname, ulen);
  549. if (err)
  550. goto out_free;
  551. err = vfat_create_shortname(dir, sbi->nls_disk, uname, ulen,
  552. msdos_name, &lcase);
  553. if (err < 0)
  554. goto out_free;
  555. else if (err == 1) {
  556. de = (struct msdos_dir_entry *)slots;
  557. err = 0;
  558. goto shortname;
  559. }
  560. /* build the entry of long file name */
  561. cksum = fat_checksum(msdos_name);
  562. *nr_slots = usize / 13;
  563. for (ps = slots, i = *nr_slots; i > 0; i--, ps++) {
  564. ps->id = i;
  565. ps->attr = ATTR_EXT;
  566. ps->reserved = 0;
  567. ps->alias_checksum = cksum;
  568. ps->start = 0;
  569. offset = (i - 1) * 13;
  570. fatwchar_to16(ps->name0_4, uname + offset, 5);
  571. fatwchar_to16(ps->name5_10, uname + offset + 5, 6);
  572. fatwchar_to16(ps->name11_12, uname + offset + 11, 2);
  573. }
  574. slots[0].id |= 0x40;
  575. de = (struct msdos_dir_entry *)ps;
  576. shortname:
  577. /* build the entry of 8.3 alias name */
  578. (*nr_slots)++;
  579. memcpy(de->name, msdos_name, MSDOS_NAME);
  580. de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
  581. de->lcase = lcase;
  582. fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
  583. de->time = de->ctime = time;
  584. de->date = de->cdate = de->adate = date;
  585. de->ctime_cs = time_cs;
  586. fat_set_start(de, cluster);
  587. de->size = 0;
  588. out_free:
  589. __putname(uname);
  590. return err;
  591. }
  592. static int vfat_add_entry(struct inode *dir, struct qstr *qname, int is_dir,
  593. int cluster, struct timespec *ts,
  594. struct fat_slot_info *sinfo)
  595. {
  596. struct msdos_dir_slot *slots;
  597. unsigned int len;
  598. int err, nr_slots;
  599. len = vfat_striptail_len(qname);
  600. if (len == 0)
  601. return -ENOENT;
  602. slots = kmalloc(sizeof(*slots) * MSDOS_SLOTS, GFP_NOFS);
  603. if (slots == NULL)
  604. return -ENOMEM;
  605. err = vfat_build_slots(dir, qname->name, len, is_dir, cluster, ts,
  606. slots, &nr_slots);
  607. if (err)
  608. goto cleanup;
  609. err = fat_add_entries(dir, slots, nr_slots, sinfo);
  610. if (err)
  611. goto cleanup;
  612. /* update timestamp */
  613. dir->i_ctime = dir->i_mtime = dir->i_atime = *ts;
  614. if (IS_DIRSYNC(dir))
  615. (void)fat_sync_inode(dir);
  616. else
  617. mark_inode_dirty(dir);
  618. cleanup:
  619. kfree(slots);
  620. return err;
  621. }
  622. static int vfat_find(struct inode *dir, struct qstr *qname,
  623. struct fat_slot_info *sinfo)
  624. {
  625. unsigned int len = vfat_striptail_len(qname);
  626. if (len == 0)
  627. return -ENOENT;
  628. return fat_search_long(dir, qname->name, len, sinfo);
  629. }
  630. /*
  631. * (nfsd's) anonymous disconnected dentry?
  632. * NOTE: !IS_ROOT() is not anonymous (I.e. d_splice_alias() did the job).
  633. */
  634. static int vfat_d_anon_disconn(struct dentry *dentry)
  635. {
  636. return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
  637. }
  638. static struct dentry *vfat_lookup(struct inode *dir, struct dentry *dentry,
  639. unsigned int flags)
  640. {
  641. struct super_block *sb = dir->i_sb;
  642. struct fat_slot_info sinfo;
  643. struct inode *inode;
  644. struct dentry *alias;
  645. int err;
  646. mutex_lock(&MSDOS_SB(sb)->s_lock);
  647. err = vfat_find(dir, &dentry->d_name, &sinfo);
  648. if (err) {
  649. if (err == -ENOENT) {
  650. inode = NULL;
  651. goto out;
  652. }
  653. goto error;
  654. }
  655. inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  656. brelse(sinfo.bh);
  657. if (IS_ERR(inode)) {
  658. err = PTR_ERR(inode);
  659. goto error;
  660. }
  661. alias = d_find_alias(inode);
  662. if (alias && !vfat_d_anon_disconn(alias)) {
  663. /*
  664. * This inode has non anonymous-DCACHE_DISCONNECTED
  665. * dentry. This means, the user did ->lookup() by an
  666. * another name (longname vs 8.3 alias of it) in past.
  667. *
  668. * Switch to new one for reason of locality if possible.
  669. */
  670. BUG_ON(d_unhashed(alias));
  671. if (!S_ISDIR(inode->i_mode))
  672. d_move(alias, dentry);
  673. iput(inode);
  674. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  675. return alias;
  676. } else
  677. dput(alias);
  678. out:
  679. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  680. dentry->d_time = dentry->d_parent->d_inode->i_version;
  681. dentry = d_splice_alias(inode, dentry);
  682. if (dentry)
  683. dentry->d_time = dentry->d_parent->d_inode->i_version;
  684. return dentry;
  685. error:
  686. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  687. return ERR_PTR(err);
  688. }
  689. static int vfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  690. bool excl)
  691. {
  692. struct super_block *sb = dir->i_sb;
  693. struct inode *inode;
  694. struct fat_slot_info sinfo;
  695. struct timespec ts;
  696. int err;
  697. mutex_lock(&MSDOS_SB(sb)->s_lock);
  698. ts = CURRENT_TIME_SEC;
  699. err = vfat_add_entry(dir, &dentry->d_name, 0, 0, &ts, &sinfo);
  700. if (err)
  701. goto out;
  702. dir->i_version++;
  703. inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  704. brelse(sinfo.bh);
  705. if (IS_ERR(inode)) {
  706. err = PTR_ERR(inode);
  707. goto out;
  708. }
  709. inode->i_version++;
  710. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  711. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  712. dentry->d_time = dentry->d_parent->d_inode->i_version;
  713. d_instantiate(dentry, inode);
  714. out:
  715. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  716. return err;
  717. }
  718. static int vfat_rmdir(struct inode *dir, struct dentry *dentry)
  719. {
  720. struct inode *inode = dentry->d_inode;
  721. struct super_block *sb = dir->i_sb;
  722. struct fat_slot_info sinfo;
  723. int err;
  724. mutex_lock(&MSDOS_SB(sb)->s_lock);
  725. err = fat_dir_empty(inode);
  726. if (err)
  727. goto out;
  728. err = vfat_find(dir, &dentry->d_name, &sinfo);
  729. if (err)
  730. goto out;
  731. err = fat_remove_entries(dir, &sinfo); /* and releases bh */
  732. if (err)
  733. goto out;
  734. drop_nlink(dir);
  735. clear_nlink(inode);
  736. inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
  737. fat_detach(inode);
  738. out:
  739. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  740. return err;
  741. }
  742. static int vfat_unlink(struct inode *dir, struct dentry *dentry)
  743. {
  744. struct inode *inode = dentry->d_inode;
  745. struct super_block *sb = dir->i_sb;
  746. struct fat_slot_info sinfo;
  747. int err;
  748. mutex_lock(&MSDOS_SB(sb)->s_lock);
  749. err = vfat_find(dir, &dentry->d_name, &sinfo);
  750. if (err)
  751. goto out;
  752. err = fat_remove_entries(dir, &sinfo); /* and releases bh */
  753. if (err)
  754. goto out;
  755. clear_nlink(inode);
  756. inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
  757. fat_detach(inode);
  758. out:
  759. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  760. return err;
  761. }
  762. static int vfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  763. {
  764. struct super_block *sb = dir->i_sb;
  765. struct inode *inode;
  766. struct fat_slot_info sinfo;
  767. struct timespec ts;
  768. int err, cluster;
  769. mutex_lock(&MSDOS_SB(sb)->s_lock);
  770. ts = CURRENT_TIME_SEC;
  771. cluster = fat_alloc_new_dir(dir, &ts);
  772. if (cluster < 0) {
  773. err = cluster;
  774. goto out;
  775. }
  776. err = vfat_add_entry(dir, &dentry->d_name, 1, cluster, &ts, &sinfo);
  777. if (err)
  778. goto out_free;
  779. dir->i_version++;
  780. inc_nlink(dir);
  781. inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  782. brelse(sinfo.bh);
  783. if (IS_ERR(inode)) {
  784. err = PTR_ERR(inode);
  785. /* the directory was completed, just return a error */
  786. goto out;
  787. }
  788. inode->i_version++;
  789. set_nlink(inode, 2);
  790. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  791. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  792. dentry->d_time = dentry->d_parent->d_inode->i_version;
  793. d_instantiate(dentry, inode);
  794. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  795. return 0;
  796. out_free:
  797. fat_free_clusters(dir, cluster);
  798. out:
  799. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  800. return err;
  801. }
  802. static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  803. struct inode *new_dir, struct dentry *new_dentry)
  804. {
  805. struct buffer_head *dotdot_bh;
  806. struct msdos_dir_entry *dotdot_de;
  807. struct inode *old_inode, *new_inode;
  808. struct fat_slot_info old_sinfo, sinfo;
  809. struct timespec ts;
  810. loff_t new_i_pos;
  811. int err, is_dir, update_dotdot, corrupt = 0;
  812. struct super_block *sb = old_dir->i_sb;
  813. old_sinfo.bh = sinfo.bh = dotdot_bh = NULL;
  814. old_inode = old_dentry->d_inode;
  815. new_inode = new_dentry->d_inode;
  816. mutex_lock(&MSDOS_SB(sb)->s_lock);
  817. err = vfat_find(old_dir, &old_dentry->d_name, &old_sinfo);
  818. if (err)
  819. goto out;
  820. is_dir = S_ISDIR(old_inode->i_mode);
  821. update_dotdot = (is_dir && old_dir != new_dir);
  822. if (update_dotdot) {
  823. if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de)) {
  824. err = -EIO;
  825. goto out;
  826. }
  827. }
  828. ts = CURRENT_TIME_SEC;
  829. if (new_inode) {
  830. if (is_dir) {
  831. err = fat_dir_empty(new_inode);
  832. if (err)
  833. goto out;
  834. }
  835. new_i_pos = MSDOS_I(new_inode)->i_pos;
  836. fat_detach(new_inode);
  837. } else {
  838. err = vfat_add_entry(new_dir, &new_dentry->d_name, is_dir, 0,
  839. &ts, &sinfo);
  840. if (err)
  841. goto out;
  842. new_i_pos = sinfo.i_pos;
  843. }
  844. new_dir->i_version++;
  845. fat_detach(old_inode);
  846. fat_attach(old_inode, new_i_pos);
  847. if (IS_DIRSYNC(new_dir)) {
  848. err = fat_sync_inode(old_inode);
  849. if (err)
  850. goto error_inode;
  851. } else
  852. mark_inode_dirty(old_inode);
  853. if (update_dotdot) {
  854. fat_set_start(dotdot_de, MSDOS_I(new_dir)->i_logstart);
  855. mark_buffer_dirty_inode(dotdot_bh, old_inode);
  856. if (IS_DIRSYNC(new_dir)) {
  857. err = sync_dirty_buffer(dotdot_bh);
  858. if (err)
  859. goto error_dotdot;
  860. }
  861. drop_nlink(old_dir);
  862. if (!new_inode)
  863. inc_nlink(new_dir);
  864. }
  865. err = fat_remove_entries(old_dir, &old_sinfo); /* and releases bh */
  866. old_sinfo.bh = NULL;
  867. if (err)
  868. goto error_dotdot;
  869. old_dir->i_version++;
  870. old_dir->i_ctime = old_dir->i_mtime = ts;
  871. if (IS_DIRSYNC(old_dir))
  872. (void)fat_sync_inode(old_dir);
  873. else
  874. mark_inode_dirty(old_dir);
  875. if (new_inode) {
  876. drop_nlink(new_inode);
  877. if (is_dir)
  878. drop_nlink(new_inode);
  879. new_inode->i_ctime = ts;
  880. }
  881. out:
  882. brelse(sinfo.bh);
  883. brelse(dotdot_bh);
  884. brelse(old_sinfo.bh);
  885. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  886. return err;
  887. error_dotdot:
  888. /* data cluster is shared, serious corruption */
  889. corrupt = 1;
  890. if (update_dotdot) {
  891. fat_set_start(dotdot_de, MSDOS_I(old_dir)->i_logstart);
  892. mark_buffer_dirty_inode(dotdot_bh, old_inode);
  893. corrupt |= sync_dirty_buffer(dotdot_bh);
  894. }
  895. error_inode:
  896. fat_detach(old_inode);
  897. fat_attach(old_inode, old_sinfo.i_pos);
  898. if (new_inode) {
  899. fat_attach(new_inode, new_i_pos);
  900. if (corrupt)
  901. corrupt |= fat_sync_inode(new_inode);
  902. } else {
  903. /*
  904. * If new entry was not sharing the data cluster, it
  905. * shouldn't be serious corruption.
  906. */
  907. int err2 = fat_remove_entries(new_dir, &sinfo);
  908. if (corrupt)
  909. corrupt |= err2;
  910. sinfo.bh = NULL;
  911. }
  912. if (corrupt < 0) {
  913. fat_fs_error(new_dir->i_sb,
  914. "%s: Filesystem corrupted (i_pos %lld)",
  915. __func__, sinfo.i_pos);
  916. }
  917. goto out;
  918. }
  919. static const struct inode_operations vfat_dir_inode_operations = {
  920. .create = vfat_create,
  921. .lookup = vfat_lookup,
  922. .unlink = vfat_unlink,
  923. .mkdir = vfat_mkdir,
  924. .rmdir = vfat_rmdir,
  925. .rename = vfat_rename,
  926. .setattr = fat_setattr,
  927. .getattr = fat_getattr,
  928. };
  929. static void setup(struct super_block *sb)
  930. {
  931. MSDOS_SB(sb)->dir_ops = &vfat_dir_inode_operations;
  932. if (MSDOS_SB(sb)->options.name_check != 's')
  933. sb->s_d_op = &vfat_ci_dentry_ops;
  934. else
  935. sb->s_d_op = &vfat_dentry_ops;
  936. }
  937. static int vfat_fill_super(struct super_block *sb, void *data, int silent)
  938. {
  939. return fat_fill_super(sb, data, silent, 1, setup);
  940. }
  941. static struct dentry *vfat_mount(struct file_system_type *fs_type,
  942. int flags, const char *dev_name,
  943. void *data)
  944. {
  945. return mount_bdev(fs_type, flags, dev_name, data, vfat_fill_super);
  946. }
  947. static struct file_system_type vfat_fs_type = {
  948. .owner = THIS_MODULE,
  949. .name = "vfat",
  950. .mount = vfat_mount,
  951. .kill_sb = kill_block_super,
  952. .fs_flags = FS_REQUIRES_DEV,
  953. };
  954. MODULE_ALIAS_FS("vfat");
  955. static int __init init_vfat_fs(void)
  956. {
  957. return register_filesystem(&vfat_fs_type);
  958. }
  959. static void __exit exit_vfat_fs(void)
  960. {
  961. unregister_filesystem(&vfat_fs_type);
  962. }
  963. MODULE_LICENSE("GPL");
  964. MODULE_DESCRIPTION("VFAT filesystem support");
  965. MODULE_AUTHOR("Gordon Chaffee");
  966. module_init(init_vfat_fs)
  967. module_exit(exit_vfat_fs)