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.

415 lines
11 KiB

include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h percpu.h is included by sched.h and module.h and thus ends up being included when building most .c files. percpu.h includes slab.h which in turn includes gfp.h making everything defined by the two files universally available and complicating inclusion dependencies. percpu.h -> slab.h dependency is about to be removed. Prepare for this change by updating users of gfp and slab facilities include those headers directly instead of assuming availability. As this conversion needs to touch large number of source files, the following script is used as the basis of conversion. http://userweb.kernel.org/~tj/misc/slabh-sweep.py The script does the followings. * Scan files for gfp and slab usages and update includes such that only the necessary includes are there. ie. if only gfp is used, gfp.h, if slab is used, slab.h. * When the script inserts a new include, it looks at the include blocks and try to put the new include such that its order conforms to its surrounding. It's put in the include block which contains core kernel includes, in the same order that the rest are ordered - alphabetical, Christmas tree, rev-Xmas-tree or at the end if there doesn't seem to be any matching order. * If the script can't find a place to put a new include (mostly because the file doesn't have fitting include block), it prints out an error message indicating which .h file needs to be added to the file. The conversion was done in the following steps. 1. The initial automatic conversion of all .c files updated slightly over 4000 files, deleting around 700 includes and adding ~480 gfp.h and ~3000 slab.h inclusions. The script emitted errors for ~400 files. 2. Each error was manually checked. Some didn't need the inclusion, some needed manual addition while adding it to implementation .h or embedding .c file was more appropriate for others. This step added inclusions to around 150 files. 3. The script was run again and the output was compared to the edits from #2 to make sure no file was left behind. 4. Several build tests were done and a couple of problems were fixed. e.g. lib/decompress_*.c used malloc/free() wrappers around slab APIs requiring slab.h to be added manually. 5. The script was run on all .h files but without automatically editing them as sprinkling gfp.h and slab.h inclusions around .h files could easily lead to inclusion dependency hell. Most gfp.h inclusion directives were ignored as stuff from gfp.h was usually wildly available and often used in preprocessor macros. Each slab.h inclusion directive was examined and added manually as necessary. 6. percpu.h was updated not to include slab.h. 7. Build test were done on the following configurations and failures were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my distributed build env didn't work with gcov compiles) and a few more options had to be turned off depending on archs to make things build (like ipr on powerpc/64 which failed due to missing writeq). * x86 and x86_64 UP and SMP allmodconfig and a custom test config. * powerpc and powerpc64 SMP allmodconfig * sparc and sparc64 SMP allmodconfig * ia64 SMP allmodconfig * s390 SMP allmodconfig * alpha SMP allmodconfig * um on x86_64 SMP allmodconfig 8. percpu.h modifications were reverted so that it could be applied as a separate patch and serve as bisection point. Given the fact that I had only a couple of failures from tests on step 6, I'm fairly confident about the coverage of this conversion patch. If there is a breakage, it's likely to be something in one of the arch headers which should be easily discoverable easily on most builds of the specific arch. Signed-off-by: Tejun Heo <tj@kernel.org> Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
12 years ago
  1. /*
  2. * fs/cifs/cifs_unicode.c
  3. *
  4. * Copyright (c) International Business Machines Corp., 2000,2009
  5. * Modified by Steve French (sfrench@us.ibm.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include "cifs_unicode.h"
  24. #include "cifs_uniupr.h"
  25. #include "cifspdu.h"
  26. #include "cifsglob.h"
  27. #include "cifs_debug.h"
  28. /*
  29. * cifs_utf16_bytes - how long will a string be after conversion?
  30. * @utf16 - pointer to input string
  31. * @maxbytes - don't go past this many bytes of input string
  32. * @codepage - destination codepage
  33. *
  34. * Walk a utf16le string and return the number of bytes that the string will
  35. * be after being converted to the given charset, not including any null
  36. * termination required. Don't walk past maxbytes in the source buffer.
  37. */
  38. int
  39. cifs_utf16_bytes(const __le16 *from, int maxbytes,
  40. const struct nls_table *codepage)
  41. {
  42. int i;
  43. int charlen, outlen = 0;
  44. int maxwords = maxbytes / 2;
  45. char tmp[NLS_MAX_CHARSET_SIZE];
  46. __u16 ftmp;
  47. for (i = 0; i < maxwords; i++) {
  48. ftmp = get_unaligned_le16(&from[i]);
  49. if (ftmp == 0)
  50. break;
  51. charlen = codepage->uni2char(ftmp, tmp, NLS_MAX_CHARSET_SIZE);
  52. if (charlen > 0)
  53. outlen += charlen;
  54. else
  55. outlen++;
  56. }
  57. return outlen;
  58. }
  59. /*
  60. * cifs_mapchar - convert a host-endian char to proper char in codepage
  61. * @target - where converted character should be copied
  62. * @src_char - 2 byte host-endian source character
  63. * @cp - codepage to which character should be converted
  64. * @mapchar - should character be mapped according to mapchars mount option?
  65. *
  66. * This function handles the conversion of a single character. It is the
  67. * responsibility of the caller to ensure that the target buffer is large
  68. * enough to hold the result of the conversion (at least NLS_MAX_CHARSET_SIZE).
  69. */
  70. static int
  71. cifs_mapchar(char *target, const __u16 src_char, const struct nls_table *cp,
  72. bool mapchar)
  73. {
  74. int len = 1;
  75. if (!mapchar)
  76. goto cp_convert;
  77. /*
  78. * BB: Cannot handle remapping UNI_SLASH until all the calls to
  79. * build_path_from_dentry are modified, as they use slash as
  80. * separator.
  81. */
  82. switch (src_char) {
  83. case UNI_COLON:
  84. *target = ':';
  85. break;
  86. case UNI_ASTERISK:
  87. *target = '*';
  88. break;
  89. case UNI_QUESTION:
  90. *target = '?';
  91. break;
  92. case UNI_PIPE:
  93. *target = '|';
  94. break;
  95. case UNI_GRTRTHAN:
  96. *target = '>';
  97. break;
  98. case UNI_LESSTHAN:
  99. *target = '<';
  100. break;
  101. default:
  102. goto cp_convert;
  103. }
  104. out:
  105. return len;
  106. cp_convert:
  107. len = cp->uni2char(src_char, target, NLS_MAX_CHARSET_SIZE);
  108. if (len <= 0) {
  109. *target = '?';
  110. len = 1;
  111. }
  112. goto out;
  113. }
  114. /*
  115. * cifs_from_utf16 - convert utf16le string to local charset
  116. * @to - destination buffer
  117. * @from - source buffer
  118. * @tolen - destination buffer size (in bytes)
  119. * @fromlen - source buffer size (in bytes)
  120. * @codepage - codepage to which characters should be converted
  121. * @mapchar - should characters be remapped according to the mapchars option?
  122. *
  123. * Convert a little-endian utf16le string (as sent by the server) to a string
  124. * in the provided codepage. The tolen and fromlen parameters are to ensure
  125. * that the code doesn't walk off of the end of the buffer (which is always
  126. * a danger if the alignment of the source buffer is off). The destination
  127. * string is always properly null terminated and fits in the destination
  128. * buffer. Returns the length of the destination string in bytes (including
  129. * null terminator).
  130. *
  131. * Note that some windows versions actually send multiword UTF-16 characters
  132. * instead of straight UTF16-2. The linux nls routines however aren't able to
  133. * deal with those characters properly. In the event that we get some of
  134. * those characters, they won't be translated properly.
  135. */
  136. int
  137. cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
  138. const struct nls_table *codepage, bool mapchar)
  139. {
  140. int i, charlen, safelen;
  141. int outlen = 0;
  142. int nullsize = nls_nullsize(codepage);
  143. int fromwords = fromlen / 2;
  144. char tmp[NLS_MAX_CHARSET_SIZE];
  145. __u16 ftmp;
  146. /*
  147. * because the chars can be of varying widths, we need to take care
  148. * not to overflow the destination buffer when we get close to the
  149. * end of it. Until we get to this offset, we don't need to check
  150. * for overflow however.
  151. */
  152. safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
  153. for (i = 0; i < fromwords; i++) {
  154. ftmp = get_unaligned_le16(&from[i]);
  155. if (ftmp == 0)
  156. break;
  157. /*
  158. * check to see if converting this character might make the
  159. * conversion bleed into the null terminator
  160. */
  161. if (outlen >= safelen) {
  162. charlen = cifs_mapchar(tmp, ftmp, codepage, mapchar);
  163. if ((outlen + charlen) > (tolen - nullsize))
  164. break;
  165. }
  166. /* put converted char into 'to' buffer */
  167. charlen = cifs_mapchar(&to[outlen], ftmp, codepage, mapchar);
  168. outlen += charlen;
  169. }
  170. /* properly null-terminate string */
  171. for (i = 0; i < nullsize; i++)
  172. to[outlen++] = 0;
  173. return outlen;
  174. }
  175. /*
  176. * NAME: cifs_strtoUTF16()
  177. *
  178. * FUNCTION: Convert character string to unicode string
  179. *
  180. */
  181. int
  182. cifs_strtoUTF16(__le16 *to, const char *from, int len,
  183. const struct nls_table *codepage)
  184. {
  185. int charlen;
  186. int i;
  187. wchar_t wchar_to; /* needed to quiet sparse */
  188. /* special case for utf8 to handle no plane0 chars */
  189. if (!strcmp(codepage->charset, "utf8")) {
  190. /*
  191. * convert utf8 -> utf16, we assume we have enough space
  192. * as caller should have assumed conversion does not overflow
  193. * in destination len is length in wchar_t units (16bits)
  194. */
  195. i = utf8s_to_utf16s(from, len, UTF16_LITTLE_ENDIAN,
  196. (wchar_t *) to, len);
  197. /* if success terminate and exit */
  198. if (i >= 0)
  199. goto success;
  200. /*
  201. * if fails fall back to UCS encoding as this
  202. * function should not return negative values
  203. * currently can fail only if source contains
  204. * invalid encoded characters
  205. */
  206. }
  207. for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
  208. charlen = codepage->char2uni(from, len, &wchar_to);
  209. if (charlen < 1) {
  210. cifs_dbg(VFS, "strtoUTF16: char2uni of 0x%x returned %d\n",
  211. *from, charlen);
  212. /* A question mark */
  213. wchar_to = 0x003f;
  214. charlen = 1;
  215. }
  216. put_unaligned_le16(wchar_to, &to[i]);
  217. }
  218. success:
  219. put_unaligned_le16(0, &to[i]);
  220. return i;
  221. }
  222. /*
  223. * cifs_strndup_from_utf16 - copy a string from wire format to the local
  224. * codepage
  225. * @src - source string
  226. * @maxlen - don't walk past this many bytes in the source string
  227. * @is_unicode - is this a unicode string?
  228. * @codepage - destination codepage
  229. *
  230. * Take a string given by the server, convert it to the local codepage and
  231. * put it in a new buffer. Returns a pointer to the new string or NULL on
  232. * error.
  233. */
  234. char *
  235. cifs_strndup_from_utf16(const char *src, const int maxlen,
  236. const bool is_unicode, const struct nls_table *codepage)
  237. {
  238. int len;
  239. char *dst;
  240. if (is_unicode) {
  241. len = cifs_utf16_bytes((__le16 *) src, maxlen, codepage);
  242. len += nls_nullsize(codepage);
  243. dst = kmalloc(len, GFP_KERNEL);
  244. if (!dst)
  245. return NULL;
  246. cifs_from_utf16(dst, (__le16 *) src, len, maxlen, codepage,
  247. false);
  248. } else {
  249. len = strnlen(src, maxlen);
  250. len++;
  251. dst = kmalloc(len, GFP_KERNEL);
  252. if (!dst)
  253. return NULL;
  254. strlcpy(dst, src, len);
  255. }
  256. return dst;
  257. }
  258. /*
  259. * Convert 16 bit Unicode pathname to wire format from string in current code
  260. * page. Conversion may involve remapping up the six characters that are
  261. * only legal in POSIX-like OS (if they are present in the string). Path
  262. * names are little endian 16 bit Unicode on the wire
  263. */
  264. int
  265. cifsConvertToUTF16(__le16 *target, const char *source, int srclen,
  266. const struct nls_table *cp, int mapChars)
  267. {
  268. int i, j, charlen;
  269. char src_char;
  270. __le16 dst_char;
  271. wchar_t tmp;
  272. if (!mapChars)
  273. return cifs_strtoUTF16(target, source, PATH_MAX, cp);
  274. for (i = 0, j = 0; i < srclen; j++) {
  275. src_char = source[i];
  276. charlen = 1;
  277. switch (src_char) {
  278. case 0:
  279. put_unaligned(0, &target[j]);
  280. goto ctoUTF16_out;
  281. case ':':
  282. dst_char = cpu_to_le16(UNI_COLON);
  283. break;
  284. case '*':
  285. dst_char = cpu_to_le16(UNI_ASTERISK);
  286. break;
  287. case '?':
  288. dst_char = cpu_to_le16(UNI_QUESTION);
  289. break;
  290. case '<':
  291. dst_char = cpu_to_le16(UNI_LESSTHAN);
  292. break;
  293. case '>':
  294. dst_char = cpu_to_le16(UNI_GRTRTHAN);
  295. break;
  296. case '|':
  297. dst_char = cpu_to_le16(UNI_PIPE);
  298. break;
  299. /*
  300. * FIXME: We can not handle remapping backslash (UNI_SLASH)
  301. * until all the calls to build_path_from_dentry are modified,
  302. * as they use backslash as separator.
  303. */
  304. default:
  305. charlen = cp->char2uni(source + i, srclen - i, &tmp);
  306. dst_char = cpu_to_le16(tmp);
  307. /*
  308. * if no match, use question mark, which at least in
  309. * some cases serves as wild card
  310. */
  311. if (charlen < 1) {
  312. dst_char = cpu_to_le16(0x003f);
  313. charlen = 1;
  314. }
  315. }
  316. /*
  317. * character may take more than one byte in the source string,
  318. * but will take exactly two bytes in the target string
  319. */
  320. i += charlen;
  321. put_unaligned(dst_char, &target[j]);
  322. }
  323. ctoUTF16_out:
  324. return j;
  325. }
  326. #ifdef CONFIG_CIFS_SMB2
  327. /*
  328. * cifs_local_to_utf16_bytes - how long will a string be after conversion?
  329. * @from - pointer to input string
  330. * @maxbytes - don't go past this many bytes of input string
  331. * @codepage - source codepage
  332. *
  333. * Walk a string and return the number of bytes that the string will
  334. * be after being converted to the given charset, not including any null
  335. * termination required. Don't walk past maxbytes in the source buffer.
  336. */
  337. static int
  338. cifs_local_to_utf16_bytes(const char *from, int len,
  339. const struct nls_table *codepage)
  340. {
  341. int charlen;
  342. int i;
  343. wchar_t wchar_to;
  344. for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
  345. charlen = codepage->char2uni(from, len, &wchar_to);
  346. /* Failed conversion defaults to a question mark */
  347. if (charlen < 1)
  348. charlen = 1;
  349. }
  350. return 2 * i; /* UTF16 characters are two bytes */
  351. }
  352. /*
  353. * cifs_strndup_to_utf16 - copy a string to wire format from the local codepage
  354. * @src - source string
  355. * @maxlen - don't walk past this many bytes in the source string
  356. * @utf16_len - the length of the allocated string in bytes (including null)
  357. * @cp - source codepage
  358. * @remap - map special chars
  359. *
  360. * Take a string convert it from the local codepage to UTF16 and
  361. * put it in a new buffer. Returns a pointer to the new string or NULL on
  362. * error.
  363. */
  364. __le16 *
  365. cifs_strndup_to_utf16(const char *src, const int maxlen, int *utf16_len,
  366. const struct nls_table *cp, int remap)
  367. {
  368. int len;
  369. __le16 *dst;
  370. len = cifs_local_to_utf16_bytes(src, maxlen, cp);
  371. len += 2; /* NULL */
  372. dst = kmalloc(len, GFP_KERNEL);
  373. if (!dst) {
  374. *utf16_len = 0;
  375. return NULL;
  376. }
  377. cifsConvertToUTF16(dst, src, strlen(src), cp, remap);
  378. *utf16_len = len;
  379. return dst;
  380. }
  381. #endif /* CONFIG_CIFS_SMB2 */