dir.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /**
  3. * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
  4. *
  5. * Copyright (c) 2001-2007 Anton Altaparmakov
  6. * Copyright (c) 2002 Richard Russon
  7. */
  8. #include <linux/buffer_head.h>
  9. #include <linux/slab.h>
  10. #include <linux/blkdev.h>
  11. #include "dir.h"
  12. #include "aops.h"
  13. #include "attrib.h"
  14. #include "mft.h"
  15. #include "debug.h"
  16. #include "ntfs.h"
  17. /**
  18. * The little endian Unicode string $I30 as a global constant.
  19. */
  20. ntfschar I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'),
  21. cpu_to_le16('3'), cpu_to_le16('0'), 0 };
  22. /**
  23. * ntfs_lookup_inode_by_name - find an inode in a directory given its name
  24. * @dir_ni: ntfs inode of the directory in which to search for the name
  25. * @uname: Unicode name for which to search in the directory
  26. * @uname_len: length of the name @uname in Unicode characters
  27. * @res: return the found file name if necessary (see below)
  28. *
  29. * Look for an inode with name @uname in the directory with inode @dir_ni.
  30. * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
  31. * the Unicode name. If the name is found in the directory, the corresponding
  32. * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
  33. * is a 64-bit number containing the sequence number.
  34. *
  35. * On error, a negative value is returned corresponding to the error code. In
  36. * particular if the inode is not found -ENOENT is returned. Note that you
  37. * can't just check the return value for being negative, you have to check the
  38. * inode number for being negative which you can extract using MREC(return
  39. * value).
  40. *
  41. * Note, @uname_len does not include the (optional) terminating NULL character.
  42. *
  43. * Note, we look for a case sensitive match first but we also look for a case
  44. * insensitive match at the same time. If we find a case insensitive match, we
  45. * save that for the case that we don't find an exact match, where we return
  46. * the case insensitive match and setup @res (which we allocate!) with the mft
  47. * reference, the file name type, length and with a copy of the little endian
  48. * Unicode file name itself. If we match a file name which is in the DOS name
  49. * space, we only return the mft reference and file name type in @res.
  50. * ntfs_lookup() then uses this to find the long file name in the inode itself.
  51. * This is to avoid polluting the dcache with short file names. We want them to
  52. * work but we don't care for how quickly one can access them. This also fixes
  53. * the dcache aliasing issues.
  54. *
  55. * Locking: - Caller must hold i_mutex on the directory.
  56. * - Each page cache page in the index allocation mapping must be
  57. * locked whilst being accessed otherwise we may find a corrupt
  58. * page due to it being under ->writepage at the moment which
  59. * applies the mst protection fixups before writing out and then
  60. * removes them again after the write is complete after which it
  61. * unlocks the page.
  62. */
  63. MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
  64. const int uname_len, ntfs_name **res)
  65. {
  66. ntfs_volume *vol = dir_ni->vol;
  67. struct super_block *sb = vol->sb;
  68. MFT_RECORD *m;
  69. INDEX_ROOT *ir;
  70. INDEX_ENTRY *ie;
  71. INDEX_ALLOCATION *ia;
  72. u8 *index_end;
  73. u64 mref;
  74. ntfs_attr_search_ctx *ctx;
  75. int err, rc;
  76. VCN vcn, old_vcn;
  77. struct address_space *ia_mapping;
  78. struct page *page;
  79. u8 *kaddr;
  80. ntfs_name *name = NULL;
  81. BUG_ON(!S_ISDIR(VFS_I(dir_ni)->i_mode));
  82. BUG_ON(NInoAttr(dir_ni));
  83. /* Get hold of the mft record for the directory. */
  84. m = map_mft_record(dir_ni);
  85. if (IS_ERR(m)) {
  86. ntfs_error(sb, "map_mft_record() failed with error code %ld.",
  87. -PTR_ERR(m));
  88. return ERR_MREF(PTR_ERR(m));
  89. }
  90. ctx = ntfs_attr_get_search_ctx(dir_ni, m);
  91. if (unlikely(!ctx)) {
  92. err = -ENOMEM;
  93. goto err_out;
  94. }
  95. /* Find the index root attribute in the mft record. */
  96. err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
  97. 0, ctx);
  98. if (unlikely(err)) {
  99. if (err == -ENOENT) {
  100. ntfs_error(sb, "Index root attribute missing in "
  101. "directory inode 0x%lx.",
  102. dir_ni->mft_no);
  103. err = -EIO;
  104. }
  105. goto err_out;
  106. }
  107. /* Get to the index root value (it's been verified in read_inode). */
  108. ir = (INDEX_ROOT*)((u8*)ctx->attr +
  109. le16_to_cpu(ctx->attr->data.resident.value_offset));
  110. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  111. /* The first index entry. */
  112. ie = (INDEX_ENTRY*)((u8*)&ir->index +
  113. le32_to_cpu(ir->index.entries_offset));
  114. /*
  115. * Loop until we exceed valid memory (corruption case) or until we
  116. * reach the last entry.
  117. */
  118. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  119. /* Bounds checks. */
  120. if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
  121. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  122. (u8*)ie + le16_to_cpu(ie->key_length) >
  123. index_end)
  124. goto dir_err_out;
  125. /*
  126. * The last entry cannot contain a name. It can however contain
  127. * a pointer to a child node in the B+tree so we just break out.
  128. */
  129. if (ie->flags & INDEX_ENTRY_END)
  130. break;
  131. /*
  132. * We perform a case sensitive comparison and if that matches
  133. * we are done and return the mft reference of the inode (i.e.
  134. * the inode number together with the sequence number for
  135. * consistency checking). We convert it to cpu format before
  136. * returning.
  137. */
  138. if (ntfs_are_names_equal(uname, uname_len,
  139. (ntfschar*)&ie->key.file_name.file_name,
  140. ie->key.file_name.file_name_length,
  141. CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
  142. found_it:
  143. /*
  144. * We have a perfect match, so we don't need to care
  145. * about having matched imperfectly before, so we can
  146. * free name and set *res to NULL.
  147. * However, if the perfect match is a short file name,
  148. * we need to signal this through *res, so that
  149. * ntfs_lookup() can fix dcache aliasing issues.
  150. * As an optimization we just reuse an existing
  151. * allocation of *res.
  152. */
  153. if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
  154. if (!name) {
  155. name = kmalloc(sizeof(ntfs_name),
  156. GFP_NOFS);
  157. if (!name) {
  158. err = -ENOMEM;
  159. goto err_out;
  160. }
  161. }
  162. name->mref = le64_to_cpu(
  163. ie->data.dir.indexed_file);
  164. name->type = FILE_NAME_DOS;
  165. name->len = 0;
  166. *res = name;
  167. } else {
  168. kfree(name);
  169. *res = NULL;
  170. }
  171. mref = le64_to_cpu(ie->data.dir.indexed_file);
  172. ntfs_attr_put_search_ctx(ctx);
  173. unmap_mft_record(dir_ni);
  174. return mref;
  175. }
  176. /*
  177. * For a case insensitive mount, we also perform a case
  178. * insensitive comparison (provided the file name is not in the
  179. * POSIX namespace). If the comparison matches, and the name is
  180. * in the WIN32 namespace, we cache the filename in *res so
  181. * that the caller, ntfs_lookup(), can work on it. If the
  182. * comparison matches, and the name is in the DOS namespace, we
  183. * only cache the mft reference and the file name type (we set
  184. * the name length to zero for simplicity).
  185. */
  186. if (!NVolCaseSensitive(vol) &&
  187. ie->key.file_name.file_name_type &&
  188. ntfs_are_names_equal(uname, uname_len,
  189. (ntfschar*)&ie->key.file_name.file_name,
  190. ie->key.file_name.file_name_length,
  191. IGNORE_CASE, vol->upcase, vol->upcase_len)) {
  192. int name_size = sizeof(ntfs_name);
  193. u8 type = ie->key.file_name.file_name_type;
  194. u8 len = ie->key.file_name.file_name_length;
  195. /* Only one case insensitive matching name allowed. */
  196. if (name) {
  197. ntfs_error(sb, "Found already allocated name "
  198. "in phase 1. Please run chkdsk "
  199. "and if that doesn't find any "
  200. "errors please report you saw "
  201. "this message to "
  202. "linux-ntfs-dev@lists."
  203. "sourceforge.net.");
  204. goto dir_err_out;
  205. }
  206. if (type != FILE_NAME_DOS)
  207. name_size += len * sizeof(ntfschar);
  208. name = kmalloc(name_size, GFP_NOFS);
  209. if (!name) {
  210. err = -ENOMEM;
  211. goto err_out;
  212. }
  213. name->mref = le64_to_cpu(ie->data.dir.indexed_file);
  214. name->type = type;
  215. if (type != FILE_NAME_DOS) {
  216. name->len = len;
  217. memcpy(name->name, ie->key.file_name.file_name,
  218. len * sizeof(ntfschar));
  219. } else
  220. name->len = 0;
  221. *res = name;
  222. }
  223. /*
  224. * Not a perfect match, need to do full blown collation so we
  225. * know which way in the B+tree we have to go.
  226. */
  227. rc = ntfs_collate_names(uname, uname_len,
  228. (ntfschar*)&ie->key.file_name.file_name,
  229. ie->key.file_name.file_name_length, 1,
  230. IGNORE_CASE, vol->upcase, vol->upcase_len);
  231. /*
  232. * If uname collates before the name of the current entry, there
  233. * is definitely no such name in this index but we might need to
  234. * descend into the B+tree so we just break out of the loop.
  235. */
  236. if (rc == -1)
  237. break;
  238. /* The names are not equal, continue the search. */
  239. if (rc)
  240. continue;
  241. /*
  242. * Names match with case insensitive comparison, now try the
  243. * case sensitive comparison, which is required for proper
  244. * collation.
  245. */
  246. rc = ntfs_collate_names(uname, uname_len,
  247. (ntfschar*)&ie->key.file_name.file_name,
  248. ie->key.file_name.file_name_length, 1,
  249. CASE_SENSITIVE, vol->upcase, vol->upcase_len);
  250. if (rc == -1)
  251. break;
  252. if (rc)
  253. continue;
  254. /*
  255. * Perfect match, this will never happen as the
  256. * ntfs_are_names_equal() call will have gotten a match but we
  257. * still treat it correctly.
  258. */
  259. goto found_it;
  260. }
  261. /*
  262. * We have finished with this index without success. Check for the
  263. * presence of a child node and if not present return -ENOENT, unless
  264. * we have got a matching name cached in name in which case return the
  265. * mft reference associated with it.
  266. */
  267. if (!(ie->flags & INDEX_ENTRY_NODE)) {
  268. if (name) {
  269. ntfs_attr_put_search_ctx(ctx);
  270. unmap_mft_record(dir_ni);
  271. return name->mref;
  272. }
  273. ntfs_debug("Entry not found.");
  274. err = -ENOENT;
  275. goto err_out;
  276. } /* Child node present, descend into it. */
  277. /* Consistency check: Verify that an index allocation exists. */
  278. if (!NInoIndexAllocPresent(dir_ni)) {
  279. ntfs_error(sb, "No index allocation attribute but index entry "
  280. "requires one. Directory inode 0x%lx is "
  281. "corrupt or driver bug.", dir_ni->mft_no);
  282. goto err_out;
  283. }
  284. /* Get the starting vcn of the index_block holding the child node. */
  285. vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
  286. ia_mapping = VFS_I(dir_ni)->i_mapping;
  287. /*
  288. * We are done with the index root and the mft record. Release them,
  289. * otherwise we deadlock with ntfs_map_page().
  290. */
  291. ntfs_attr_put_search_ctx(ctx);
  292. unmap_mft_record(dir_ni);
  293. m = NULL;
  294. ctx = NULL;
  295. descend_into_child_node:
  296. /*
  297. * Convert vcn to index into the index allocation attribute in units
  298. * of PAGE_SIZE and map the page cache page, reading it from
  299. * disk if necessary.
  300. */
  301. page = ntfs_map_page(ia_mapping, vcn <<
  302. dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
  303. if (IS_ERR(page)) {
  304. ntfs_error(sb, "Failed to map directory index page, error %ld.",
  305. -PTR_ERR(page));
  306. err = PTR_ERR(page);
  307. goto err_out;
  308. }
  309. lock_page(page);
  310. kaddr = (u8*)page_address(page);
  311. fast_descend_into_child_node:
  312. /* Get to the index allocation block. */
  313. ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
  314. dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
  315. /* Bounds checks. */
  316. if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
  317. ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
  318. "inode 0x%lx or driver bug.", dir_ni->mft_no);
  319. goto unm_err_out;
  320. }
  321. /* Catch multi sector transfer fixup errors. */
  322. if (unlikely(!ntfs_is_indx_record(ia->magic))) {
  323. ntfs_error(sb, "Directory index record with vcn 0x%llx is "
  324. "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
  325. (unsigned long long)vcn, dir_ni->mft_no);
  326. goto unm_err_out;
  327. }
  328. if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
  329. ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
  330. "different from expected VCN (0x%llx). "
  331. "Directory inode 0x%lx is corrupt or driver "
  332. "bug.", (unsigned long long)
  333. sle64_to_cpu(ia->index_block_vcn),
  334. (unsigned long long)vcn, dir_ni->mft_no);
  335. goto unm_err_out;
  336. }
  337. if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
  338. dir_ni->itype.index.block_size) {
  339. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  340. "0x%lx has a size (%u) differing from the "
  341. "directory specified size (%u). Directory "
  342. "inode is corrupt or driver bug.",
  343. (unsigned long long)vcn, dir_ni->mft_no,
  344. le32_to_cpu(ia->index.allocated_size) + 0x18,
  345. dir_ni->itype.index.block_size);
  346. goto unm_err_out;
  347. }
  348. index_end = (u8*)ia + dir_ni->itype.index.block_size;
  349. if (index_end > kaddr + PAGE_SIZE) {
  350. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  351. "0x%lx crosses page boundary. Impossible! "
  352. "Cannot access! This is probably a bug in the "
  353. "driver.", (unsigned long long)vcn,
  354. dir_ni->mft_no);
  355. goto unm_err_out;
  356. }
  357. index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
  358. if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
  359. ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
  360. "inode 0x%lx exceeds maximum size.",
  361. (unsigned long long)vcn, dir_ni->mft_no);
  362. goto unm_err_out;
  363. }
  364. /* The first index entry. */
  365. ie = (INDEX_ENTRY*)((u8*)&ia->index +
  366. le32_to_cpu(ia->index.entries_offset));
  367. /*
  368. * Iterate similar to above big loop but applied to index buffer, thus
  369. * loop until we exceed valid memory (corruption case) or until we
  370. * reach the last entry.
  371. */
  372. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  373. /* Bounds check. */
  374. if ((u8*)ie < (u8*)ia || (u8*)ie +
  375. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  376. (u8*)ie + le16_to_cpu(ie->key_length) >
  377. index_end) {
  378. ntfs_error(sb, "Index entry out of bounds in "
  379. "directory inode 0x%lx.",
  380. dir_ni->mft_no);
  381. goto unm_err_out;
  382. }
  383. /*
  384. * The last entry cannot contain a name. It can however contain
  385. * a pointer to a child node in the B+tree so we just break out.
  386. */
  387. if (ie->flags & INDEX_ENTRY_END)
  388. break;
  389. /*
  390. * We perform a case sensitive comparison and if that matches
  391. * we are done and return the mft reference of the inode (i.e.
  392. * the inode number together with the sequence number for
  393. * consistency checking). We convert it to cpu format before
  394. * returning.
  395. */
  396. if (ntfs_are_names_equal(uname, uname_len,
  397. (ntfschar*)&ie->key.file_name.file_name,
  398. ie->key.file_name.file_name_length,
  399. CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
  400. found_it2:
  401. /*
  402. * We have a perfect match, so we don't need to care
  403. * about having matched imperfectly before, so we can
  404. * free name and set *res to NULL.
  405. * However, if the perfect match is a short file name,
  406. * we need to signal this through *res, so that
  407. * ntfs_lookup() can fix dcache aliasing issues.
  408. * As an optimization we just reuse an existing
  409. * allocation of *res.
  410. */
  411. if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
  412. if (!name) {
  413. name = kmalloc(sizeof(ntfs_name),
  414. GFP_NOFS);
  415. if (!name) {
  416. err = -ENOMEM;
  417. goto unm_err_out;
  418. }
  419. }
  420. name->mref = le64_to_cpu(
  421. ie->data.dir.indexed_file);
  422. name->type = FILE_NAME_DOS;
  423. name->len = 0;
  424. *res = name;
  425. } else {
  426. kfree(name);
  427. *res = NULL;
  428. }
  429. mref = le64_to_cpu(ie->data.dir.indexed_file);
  430. unlock_page(page);
  431. ntfs_unmap_page(page);
  432. return mref;
  433. }
  434. /*
  435. * For a case insensitive mount, we also perform a case
  436. * insensitive comparison (provided the file name is not in the
  437. * POSIX namespace). If the comparison matches, and the name is
  438. * in the WIN32 namespace, we cache the filename in *res so
  439. * that the caller, ntfs_lookup(), can work on it. If the
  440. * comparison matches, and the name is in the DOS namespace, we
  441. * only cache the mft reference and the file name type (we set
  442. * the name length to zero for simplicity).
  443. */
  444. if (!NVolCaseSensitive(vol) &&
  445. ie->key.file_name.file_name_type &&
  446. ntfs_are_names_equal(uname, uname_len,
  447. (ntfschar*)&ie->key.file_name.file_name,
  448. ie->key.file_name.file_name_length,
  449. IGNORE_CASE, vol->upcase, vol->upcase_len)) {
  450. int name_size = sizeof(ntfs_name);
  451. u8 type = ie->key.file_name.file_name_type;
  452. u8 len = ie->key.file_name.file_name_length;
  453. /* Only one case insensitive matching name allowed. */
  454. if (name) {
  455. ntfs_error(sb, "Found already allocated name "
  456. "in phase 2. Please run chkdsk "
  457. "and if that doesn't find any "
  458. "errors please report you saw "
  459. "this message to "
  460. "linux-ntfs-dev@lists."
  461. "sourceforge.net.");
  462. unlock_page(page);
  463. ntfs_unmap_page(page);
  464. goto dir_err_out;
  465. }
  466. if (type != FILE_NAME_DOS)
  467. name_size += len * sizeof(ntfschar);
  468. name = kmalloc(name_size, GFP_NOFS);
  469. if (!name) {
  470. err = -ENOMEM;
  471. goto unm_err_out;
  472. }
  473. name->mref = le64_to_cpu(ie->data.dir.indexed_file);
  474. name->type = type;
  475. if (type != FILE_NAME_DOS) {
  476. name->len = len;
  477. memcpy(name->name, ie->key.file_name.file_name,
  478. len * sizeof(ntfschar));
  479. } else
  480. name->len = 0;
  481. *res = name;
  482. }
  483. /*
  484. * Not a perfect match, need to do full blown collation so we
  485. * know which way in the B+tree we have to go.
  486. */
  487. rc = ntfs_collate_names(uname, uname_len,
  488. (ntfschar*)&ie->key.file_name.file_name,
  489. ie->key.file_name.file_name_length, 1,
  490. IGNORE_CASE, vol->upcase, vol->upcase_len);
  491. /*
  492. * If uname collates before the name of the current entry, there
  493. * is definitely no such name in this index but we might need to
  494. * descend into the B+tree so we just break out of the loop.
  495. */
  496. if (rc == -1)
  497. break;
  498. /* The names are not equal, continue the search. */
  499. if (rc)
  500. continue;
  501. /*
  502. * Names match with case insensitive comparison, now try the
  503. * case sensitive comparison, which is required for proper
  504. * collation.
  505. */
  506. rc = ntfs_collate_names(uname, uname_len,
  507. (ntfschar*)&ie->key.file_name.file_name,
  508. ie->key.file_name.file_name_length, 1,
  509. CASE_SENSITIVE, vol->upcase, vol->upcase_len);
  510. if (rc == -1)
  511. break;
  512. if (rc)
  513. continue;
  514. /*
  515. * Perfect match, this will never happen as the
  516. * ntfs_are_names_equal() call will have gotten a match but we
  517. * still treat it correctly.
  518. */
  519. goto found_it2;
  520. }
  521. /*
  522. * We have finished with this index buffer without success. Check for
  523. * the presence of a child node.
  524. */
  525. if (ie->flags & INDEX_ENTRY_NODE) {
  526. if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
  527. ntfs_error(sb, "Index entry with child node found in "
  528. "a leaf node in directory inode 0x%lx.",
  529. dir_ni->mft_no);
  530. goto unm_err_out;
  531. }
  532. /* Child node present, descend into it. */
  533. old_vcn = vcn;
  534. vcn = sle64_to_cpup((sle64*)((u8*)ie +
  535. le16_to_cpu(ie->length) - 8));
  536. if (vcn >= 0) {
  537. /* If vcn is in the same page cache page as old_vcn we
  538. * recycle the mapped page. */
  539. if (old_vcn << vol->cluster_size_bits >>
  540. PAGE_SHIFT == vcn <<
  541. vol->cluster_size_bits >>
  542. PAGE_SHIFT)
  543. goto fast_descend_into_child_node;
  544. unlock_page(page);
  545. ntfs_unmap_page(page);
  546. goto descend_into_child_node;
  547. }
  548. ntfs_error(sb, "Negative child node vcn in directory inode "
  549. "0x%lx.", dir_ni->mft_no);
  550. goto unm_err_out;
  551. }
  552. /*
  553. * No child node present, return -ENOENT, unless we have got a matching
  554. * name cached in name in which case return the mft reference
  555. * associated with it.
  556. */
  557. if (name) {
  558. unlock_page(page);
  559. ntfs_unmap_page(page);
  560. return name->mref;
  561. }
  562. ntfs_debug("Entry not found.");
  563. err = -ENOENT;
  564. unm_err_out:
  565. unlock_page(page);
  566. ntfs_unmap_page(page);
  567. err_out:
  568. if (!err)
  569. err = -EIO;
  570. if (ctx)
  571. ntfs_attr_put_search_ctx(ctx);
  572. if (m)
  573. unmap_mft_record(dir_ni);
  574. if (name) {
  575. kfree(name);
  576. *res = NULL;
  577. }
  578. return ERR_MREF(err);
  579. dir_err_out:
  580. ntfs_error(sb, "Corrupt directory. Aborting lookup.");
  581. goto err_out;
  582. }
  583. #if 0
  584. // TODO: (AIA)
  585. // The algorithm embedded in this code will be required for the time when we
  586. // want to support adding of entries to directories, where we require correct
  587. // collation of file names in order not to cause corruption of the filesystem.
  588. /**
  589. * ntfs_lookup_inode_by_name - find an inode in a directory given its name
  590. * @dir_ni: ntfs inode of the directory in which to search for the name
  591. * @uname: Unicode name for which to search in the directory
  592. * @uname_len: length of the name @uname in Unicode characters
  593. *
  594. * Look for an inode with name @uname in the directory with inode @dir_ni.
  595. * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
  596. * the Unicode name. If the name is found in the directory, the corresponding
  597. * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
  598. * is a 64-bit number containing the sequence number.
  599. *
  600. * On error, a negative value is returned corresponding to the error code. In
  601. * particular if the inode is not found -ENOENT is returned. Note that you
  602. * can't just check the return value for being negative, you have to check the
  603. * inode number for being negative which you can extract using MREC(return
  604. * value).
  605. *
  606. * Note, @uname_len does not include the (optional) terminating NULL character.
  607. */
  608. u64 ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
  609. const int uname_len)
  610. {
  611. ntfs_volume *vol = dir_ni->vol;
  612. struct super_block *sb = vol->sb;
  613. MFT_RECORD *m;
  614. INDEX_ROOT *ir;
  615. INDEX_ENTRY *ie;
  616. INDEX_ALLOCATION *ia;
  617. u8 *index_end;
  618. u64 mref;
  619. ntfs_attr_search_ctx *ctx;
  620. int err, rc;
  621. IGNORE_CASE_BOOL ic;
  622. VCN vcn, old_vcn;
  623. struct address_space *ia_mapping;
  624. struct page *page;
  625. u8 *kaddr;
  626. /* Get hold of the mft record for the directory. */
  627. m = map_mft_record(dir_ni);
  628. if (IS_ERR(m)) {
  629. ntfs_error(sb, "map_mft_record() failed with error code %ld.",
  630. -PTR_ERR(m));
  631. return ERR_MREF(PTR_ERR(m));
  632. }
  633. ctx = ntfs_attr_get_search_ctx(dir_ni, m);
  634. if (!ctx) {
  635. err = -ENOMEM;
  636. goto err_out;
  637. }
  638. /* Find the index root attribute in the mft record. */
  639. err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
  640. 0, ctx);
  641. if (unlikely(err)) {
  642. if (err == -ENOENT) {
  643. ntfs_error(sb, "Index root attribute missing in "
  644. "directory inode 0x%lx.",
  645. dir_ni->mft_no);
  646. err = -EIO;
  647. }
  648. goto err_out;
  649. }
  650. /* Get to the index root value (it's been verified in read_inode). */
  651. ir = (INDEX_ROOT*)((u8*)ctx->attr +
  652. le16_to_cpu(ctx->attr->data.resident.value_offset));
  653. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  654. /* The first index entry. */
  655. ie = (INDEX_ENTRY*)((u8*)&ir->index +
  656. le32_to_cpu(ir->index.entries_offset));
  657. /*
  658. * Loop until we exceed valid memory (corruption case) or until we
  659. * reach the last entry.
  660. */
  661. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  662. /* Bounds checks. */
  663. if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
  664. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  665. (u8*)ie + le16_to_cpu(ie->key_length) >
  666. index_end)
  667. goto dir_err_out;
  668. /*
  669. * The last entry cannot contain a name. It can however contain
  670. * a pointer to a child node in the B+tree so we just break out.
  671. */
  672. if (ie->flags & INDEX_ENTRY_END)
  673. break;
  674. /*
  675. * If the current entry has a name type of POSIX, the name is
  676. * case sensitive and not otherwise. This has the effect of us
  677. * not being able to access any POSIX file names which collate
  678. * after the non-POSIX one when they only differ in case, but
  679. * anyone doing screwy stuff like that deserves to burn in
  680. * hell... Doing that kind of stuff on NT4 actually causes
  681. * corruption on the partition even when using SP6a and Linux
  682. * is not involved at all.
  683. */
  684. ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
  685. CASE_SENSITIVE;
  686. /*
  687. * If the names match perfectly, we are done and return the
  688. * mft reference of the inode (i.e. the inode number together
  689. * with the sequence number for consistency checking. We
  690. * convert it to cpu format before returning.
  691. */
  692. if (ntfs_are_names_equal(uname, uname_len,
  693. (ntfschar*)&ie->key.file_name.file_name,
  694. ie->key.file_name.file_name_length, ic,
  695. vol->upcase, vol->upcase_len)) {
  696. found_it:
  697. mref = le64_to_cpu(ie->data.dir.indexed_file);
  698. ntfs_attr_put_search_ctx(ctx);
  699. unmap_mft_record(dir_ni);
  700. return mref;
  701. }
  702. /*
  703. * Not a perfect match, need to do full blown collation so we
  704. * know which way in the B+tree we have to go.
  705. */
  706. rc = ntfs_collate_names(uname, uname_len,
  707. (ntfschar*)&ie->key.file_name.file_name,
  708. ie->key.file_name.file_name_length, 1,
  709. IGNORE_CASE, vol->upcase, vol->upcase_len);
  710. /*
  711. * If uname collates before the name of the current entry, there
  712. * is definitely no such name in this index but we might need to
  713. * descend into the B+tree so we just break out of the loop.
  714. */
  715. if (rc == -1)
  716. break;
  717. /* The names are not equal, continue the search. */
  718. if (rc)
  719. continue;
  720. /*
  721. * Names match with case insensitive comparison, now try the
  722. * case sensitive comparison, which is required for proper
  723. * collation.
  724. */
  725. rc = ntfs_collate_names(uname, uname_len,
  726. (ntfschar*)&ie->key.file_name.file_name,
  727. ie->key.file_name.file_name_length, 1,
  728. CASE_SENSITIVE, vol->upcase, vol->upcase_len);
  729. if (rc == -1)
  730. break;
  731. if (rc)
  732. continue;
  733. /*
  734. * Perfect match, this will never happen as the
  735. * ntfs_are_names_equal() call will have gotten a match but we
  736. * still treat it correctly.
  737. */
  738. goto found_it;
  739. }
  740. /*
  741. * We have finished with this index without success. Check for the
  742. * presence of a child node.
  743. */
  744. if (!(ie->flags & INDEX_ENTRY_NODE)) {
  745. /* No child node, return -ENOENT. */
  746. err = -ENOENT;
  747. goto err_out;
  748. } /* Child node present, descend into it. */
  749. /* Consistency check: Verify that an index allocation exists. */
  750. if (!NInoIndexAllocPresent(dir_ni)) {
  751. ntfs_error(sb, "No index allocation attribute but index entry "
  752. "requires one. Directory inode 0x%lx is "
  753. "corrupt or driver bug.", dir_ni->mft_no);
  754. goto err_out;
  755. }
  756. /* Get the starting vcn of the index_block holding the child node. */
  757. vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
  758. ia_mapping = VFS_I(dir_ni)->i_mapping;
  759. /*
  760. * We are done with the index root and the mft record. Release them,
  761. * otherwise we deadlock with ntfs_map_page().
  762. */
  763. ntfs_attr_put_search_ctx(ctx);
  764. unmap_mft_record(dir_ni);
  765. m = NULL;
  766. ctx = NULL;
  767. descend_into_child_node:
  768. /*
  769. * Convert vcn to index into the index allocation attribute in units
  770. * of PAGE_SIZE and map the page cache page, reading it from
  771. * disk if necessary.
  772. */
  773. page = ntfs_map_page(ia_mapping, vcn <<
  774. dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
  775. if (IS_ERR(page)) {
  776. ntfs_error(sb, "Failed to map directory index page, error %ld.",
  777. -PTR_ERR(page));
  778. err = PTR_ERR(page);
  779. goto err_out;
  780. }
  781. lock_page(page);
  782. kaddr = (u8*)page_address(page);
  783. fast_descend_into_child_node:
  784. /* Get to the index allocation block. */
  785. ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
  786. dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
  787. /* Bounds checks. */
  788. if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
  789. ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
  790. "inode 0x%lx or driver bug.", dir_ni->mft_no);
  791. goto unm_err_out;
  792. }
  793. /* Catch multi sector transfer fixup errors. */
  794. if (unlikely(!ntfs_is_indx_record(ia->magic))) {
  795. ntfs_error(sb, "Directory index record with vcn 0x%llx is "
  796. "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
  797. (unsigned long long)vcn, dir_ni->mft_no);
  798. goto unm_err_out;
  799. }
  800. if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
  801. ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
  802. "different from expected VCN (0x%llx). "
  803. "Directory inode 0x%lx is corrupt or driver "
  804. "bug.", (unsigned long long)
  805. sle64_to_cpu(ia->index_block_vcn),
  806. (unsigned long long)vcn, dir_ni->mft_no);
  807. goto unm_err_out;
  808. }
  809. if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
  810. dir_ni->itype.index.block_size) {
  811. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  812. "0x%lx has a size (%u) differing from the "
  813. "directory specified size (%u). Directory "
  814. "inode is corrupt or driver bug.",
  815. (unsigned long long)vcn, dir_ni->mft_no,
  816. le32_to_cpu(ia->index.allocated_size) + 0x18,
  817. dir_ni->itype.index.block_size);
  818. goto unm_err_out;
  819. }
  820. index_end = (u8*)ia + dir_ni->itype.index.block_size;
  821. if (index_end > kaddr + PAGE_SIZE) {
  822. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  823. "0x%lx crosses page boundary. Impossible! "
  824. "Cannot access! This is probably a bug in the "
  825. "driver.", (unsigned long long)vcn,
  826. dir_ni->mft_no);
  827. goto unm_err_out;
  828. }
  829. index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
  830. if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
  831. ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
  832. "inode 0x%lx exceeds maximum size.",
  833. (unsigned long long)vcn, dir_ni->mft_no);
  834. goto unm_err_out;
  835. }
  836. /* The first index entry. */
  837. ie = (INDEX_ENTRY*)((u8*)&ia->index +
  838. le32_to_cpu(ia->index.entries_offset));
  839. /*
  840. * Iterate similar to above big loop but applied to index buffer, thus
  841. * loop until we exceed valid memory (corruption case) or until we
  842. * reach the last entry.
  843. */
  844. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  845. /* Bounds check. */
  846. if ((u8*)ie < (u8*)ia || (u8*)ie +
  847. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  848. (u8*)ie + le16_to_cpu(ie->key_length) >
  849. index_end) {
  850. ntfs_error(sb, "Index entry out of bounds in "
  851. "directory inode 0x%lx.",
  852. dir_ni->mft_no);
  853. goto unm_err_out;
  854. }
  855. /*
  856. * The last entry cannot contain a name. It can however contain
  857. * a pointer to a child node in the B+tree so we just break out.
  858. */
  859. if (ie->flags & INDEX_ENTRY_END)
  860. break;
  861. /*
  862. * If the current entry has a name type of POSIX, the name is
  863. * case sensitive and not otherwise. This has the effect of us
  864. * not being able to access any POSIX file names which collate
  865. * after the non-POSIX one when they only differ in case, but
  866. * anyone doing screwy stuff like that deserves to burn in
  867. * hell... Doing that kind of stuff on NT4 actually causes
  868. * corruption on the partition even when using SP6a and Linux
  869. * is not involved at all.
  870. */
  871. ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
  872. CASE_SENSITIVE;
  873. /*
  874. * If the names match perfectly, we are done and return the
  875. * mft reference of the inode (i.e. the inode number together
  876. * with the sequence number for consistency checking. We
  877. * convert it to cpu format before returning.
  878. */
  879. if (ntfs_are_names_equal(uname, uname_len,
  880. (ntfschar*)&ie->key.file_name.file_name,
  881. ie->key.file_name.file_name_length, ic,
  882. vol->upcase, vol->upcase_len)) {
  883. found_it2:
  884. mref = le64_to_cpu(ie->data.dir.indexed_file);
  885. unlock_page(page);
  886. ntfs_unmap_page(page);
  887. return mref;
  888. }
  889. /*
  890. * Not a perfect match, need to do full blown collation so we
  891. * know which way in the B+tree we have to go.
  892. */
  893. rc = ntfs_collate_names(uname, uname_len,
  894. (ntfschar*)&ie->key.file_name.file_name,
  895. ie->key.file_name.file_name_length, 1,
  896. IGNORE_CASE, vol->upcase, vol->upcase_len);
  897. /*
  898. * If uname collates before the name of the current entry, there
  899. * is definitely no such name in this index but we might need to
  900. * descend into the B+tree so we just break out of the loop.
  901. */
  902. if (rc == -1)
  903. break;
  904. /* The names are not equal, continue the search. */
  905. if (rc)
  906. continue;
  907. /*
  908. * Names match with case insensitive comparison, now try the
  909. * case sensitive comparison, which is required for proper
  910. * collation.
  911. */
  912. rc = ntfs_collate_names(uname, uname_len,
  913. (ntfschar*)&ie->key.file_name.file_name,
  914. ie->key.file_name.file_name_length, 1,
  915. CASE_SENSITIVE, vol->upcase, vol->upcase_len);
  916. if (rc == -1)
  917. break;
  918. if (rc)
  919. continue;
  920. /*
  921. * Perfect match, this will never happen as the
  922. * ntfs_are_names_equal() call will have gotten a match but we
  923. * still treat it correctly.
  924. */
  925. goto found_it2;
  926. }
  927. /*
  928. * We have finished with this index buffer without success. Check for
  929. * the presence of a child node.
  930. */
  931. if (ie->flags & INDEX_ENTRY_NODE) {
  932. if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
  933. ntfs_error(sb, "Index entry with child node found in "
  934. "a leaf node in directory inode 0x%lx.",
  935. dir_ni->mft_no);
  936. goto unm_err_out;
  937. }
  938. /* Child node present, descend into it. */
  939. old_vcn = vcn;
  940. vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
  941. if (vcn >= 0) {
  942. /* If vcn is in the same page cache page as old_vcn we
  943. * recycle the mapped page. */
  944. if (old_vcn << vol->cluster_size_bits >>
  945. PAGE_SHIFT == vcn <<
  946. vol->cluster_size_bits >>
  947. PAGE_SHIFT)
  948. goto fast_descend_into_child_node;
  949. unlock_page(page);
  950. ntfs_unmap_page(page);
  951. goto descend_into_child_node;
  952. }
  953. ntfs_error(sb, "Negative child node vcn in directory inode "
  954. "0x%lx.", dir_ni->mft_no);
  955. goto unm_err_out;
  956. }
  957. /* No child node, return -ENOENT. */
  958. ntfs_debug("Entry not found.");
  959. err = -ENOENT;
  960. unm_err_out:
  961. unlock_page(page);
  962. ntfs_unmap_page(page);
  963. err_out:
  964. if (!err)
  965. err = -EIO;
  966. if (ctx)
  967. ntfs_attr_put_search_ctx(ctx);
  968. if (m)
  969. unmap_mft_record(dir_ni);
  970. return ERR_MREF(err);
  971. dir_err_out:
  972. ntfs_error(sb, "Corrupt directory. Aborting lookup.");
  973. goto err_out;
  974. }
  975. #endif
  976. /**
  977. * ntfs_filldir - ntfs specific filldir method
  978. * @vol: current ntfs volume
  979. * @ndir: ntfs inode of current directory
  980. * @ia_page: page in which the index allocation buffer @ie is in resides
  981. * @ie: current index entry
  982. * @name: buffer to use for the converted name
  983. * @actor: what to feed the entries to
  984. *
  985. * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
  986. * callback.
  987. *
  988. * If @ia_page is not NULL it is the locked page containing the index
  989. * allocation block containing the index entry @ie.
  990. *
  991. * Note, we drop (and then reacquire) the page lock on @ia_page across the
  992. * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup
  993. * since ntfs_lookup() will lock the same page. As an optimization, we do not
  994. * retake the lock if we are returning a non-zero value as ntfs_readdir()
  995. * would need to drop the lock immediately anyway.
  996. */
  997. static inline int ntfs_filldir(ntfs_volume *vol,
  998. ntfs_inode *ndir, struct page *ia_page, INDEX_ENTRY *ie,
  999. u8 *name, struct dir_context *actor)
  1000. {
  1001. unsigned long mref;
  1002. int name_len;
  1003. unsigned dt_type;
  1004. FILE_NAME_TYPE_FLAGS name_type;
  1005. name_type = ie->key.file_name.file_name_type;
  1006. if (name_type == FILE_NAME_DOS) {
  1007. ntfs_debug("Skipping DOS name space entry.");
  1008. return 0;
  1009. }
  1010. if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) {
  1011. ntfs_debug("Skipping root directory self reference entry.");
  1012. return 0;
  1013. }
  1014. if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user &&
  1015. !NVolShowSystemFiles(vol)) {
  1016. ntfs_debug("Skipping system file.");
  1017. return 0;
  1018. }
  1019. name_len = ntfs_ucstonls(vol, (ntfschar*)&ie->key.file_name.file_name,
  1020. ie->key.file_name.file_name_length, &name,
  1021. NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
  1022. if (name_len <= 0) {
  1023. ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.",
  1024. (long long)MREF_LE(ie->data.dir.indexed_file));
  1025. return 0;
  1026. }
  1027. if (ie->key.file_name.file_attributes &
  1028. FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)
  1029. dt_type = DT_DIR;
  1030. else
  1031. dt_type = DT_REG;
  1032. mref = MREF_LE(ie->data.dir.indexed_file);
  1033. /*
  1034. * Drop the page lock otherwise we deadlock with NFS when it calls
  1035. * ->lookup since ntfs_lookup() will lock the same page.
  1036. */
  1037. if (ia_page)
  1038. unlock_page(ia_page);
  1039. ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode "
  1040. "0x%lx, DT_%s.", name, name_len, actor->pos, mref,
  1041. dt_type == DT_DIR ? "DIR" : "REG");
  1042. if (!dir_emit(actor, name, name_len, mref, dt_type))
  1043. return 1;
  1044. /* Relock the page but not if we are aborting ->readdir. */
  1045. if (ia_page)
  1046. lock_page(ia_page);
  1047. return 0;
  1048. }
  1049. /*
  1050. * We use the same basic approach as the old NTFS driver, i.e. we parse the
  1051. * index root entries and then the index allocation entries that are marked
  1052. * as in use in the index bitmap.
  1053. *
  1054. * While this will return the names in random order this doesn't matter for
  1055. * ->readdir but OTOH results in a faster ->readdir.
  1056. *
  1057. * VFS calls ->readdir without BKL but with i_mutex held. This protects the VFS
  1058. * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
  1059. * modifications).
  1060. *
  1061. * Locking: - Caller must hold i_mutex on the directory.
  1062. * - Each page cache page in the index allocation mapping must be
  1063. * locked whilst being accessed otherwise we may find a corrupt
  1064. * page due to it being under ->writepage at the moment which
  1065. * applies the mst protection fixups before writing out and then
  1066. * removes them again after the write is complete after which it
  1067. * unlocks the page.
  1068. */
  1069. static int ntfs_readdir(struct file *file, struct dir_context *actor)
  1070. {
  1071. s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
  1072. loff_t i_size;
  1073. struct inode *bmp_vi, *vdir = file_inode(file);
  1074. struct super_block *sb = vdir->i_sb;
  1075. ntfs_inode *ndir = NTFS_I(vdir);
  1076. ntfs_volume *vol = NTFS_SB(sb);
  1077. MFT_RECORD *m;
  1078. INDEX_ROOT *ir = NULL;
  1079. INDEX_ENTRY *ie;
  1080. INDEX_ALLOCATION *ia;
  1081. u8 *name = NULL;
  1082. int rc, err, ir_pos, cur_bmp_pos;
  1083. struct address_space *ia_mapping, *bmp_mapping;
  1084. struct page *bmp_page = NULL, *ia_page = NULL;
  1085. u8 *kaddr, *bmp, *index_end;
  1086. ntfs_attr_search_ctx *ctx;
  1087. ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.",
  1088. vdir->i_ino, actor->pos);
  1089. rc = err = 0;
  1090. /* Are we at end of dir yet? */
  1091. i_size = i_size_read(vdir);
  1092. if (actor->pos >= i_size + vol->mft_record_size)
  1093. return 0;
  1094. /* Emulate . and .. for all directories. */
  1095. if (!dir_emit_dots(file, actor))
  1096. return 0;
  1097. m = NULL;
  1098. ctx = NULL;
  1099. /*
  1100. * Allocate a buffer to store the current name being processed
  1101. * converted to format determined by current NLS.
  1102. */
  1103. name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS);
  1104. if (unlikely(!name)) {
  1105. err = -ENOMEM;
  1106. goto err_out;
  1107. }
  1108. /* Are we jumping straight into the index allocation attribute? */
  1109. if (actor->pos >= vol->mft_record_size)
  1110. goto skip_index_root;
  1111. /* Get hold of the mft record for the directory. */
  1112. m = map_mft_record(ndir);
  1113. if (IS_ERR(m)) {
  1114. err = PTR_ERR(m);
  1115. m = NULL;
  1116. goto err_out;
  1117. }
  1118. ctx = ntfs_attr_get_search_ctx(ndir, m);
  1119. if (unlikely(!ctx)) {
  1120. err = -ENOMEM;
  1121. goto err_out;
  1122. }
  1123. /* Get the offset into the index root attribute. */
  1124. ir_pos = (s64)actor->pos;
  1125. /* Find the index root attribute in the mft record. */
  1126. err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
  1127. 0, ctx);
  1128. if (unlikely(err)) {
  1129. ntfs_error(sb, "Index root attribute missing in directory "
  1130. "inode 0x%lx.", vdir->i_ino);
  1131. goto err_out;
  1132. }
  1133. /*
  1134. * Copy the index root attribute value to a buffer so that we can put
  1135. * the search context and unmap the mft record before calling the
  1136. * filldir() callback. We need to do this because of NFSd which calls
  1137. * ->lookup() from its filldir callback() and this causes NTFS to
  1138. * deadlock as ntfs_lookup() maps the mft record of the directory and
  1139. * we have got it mapped here already. The only solution is for us to
  1140. * unmap the mft record here so that a call to ntfs_lookup() is able to
  1141. * map the mft record without deadlocking.
  1142. */
  1143. rc = le32_to_cpu(ctx->attr->data.resident.value_length);
  1144. ir = kmalloc(rc, GFP_NOFS);
  1145. if (unlikely(!ir)) {
  1146. err = -ENOMEM;
  1147. goto err_out;
  1148. }
  1149. /* Copy the index root value (it has been verified in read_inode). */
  1150. memcpy(ir, (u8*)ctx->attr +
  1151. le16_to_cpu(ctx->attr->data.resident.value_offset), rc);
  1152. ntfs_attr_put_search_ctx(ctx);
  1153. unmap_mft_record(ndir);
  1154. ctx = NULL;
  1155. m = NULL;
  1156. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  1157. /* The first index entry. */
  1158. ie = (INDEX_ENTRY*)((u8*)&ir->index +
  1159. le32_to_cpu(ir->index.entries_offset));
  1160. /*
  1161. * Loop until we exceed valid memory (corruption case) or until we
  1162. * reach the last entry or until filldir tells us it has had enough
  1163. * or signals an error (both covered by the rc test).
  1164. */
  1165. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  1166. ntfs_debug("In index root, offset 0x%zx.", (u8*)ie - (u8*)ir);
  1167. /* Bounds checks. */
  1168. if (unlikely((u8*)ie < (u8*)ir || (u8*)ie +
  1169. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  1170. (u8*)ie + le16_to_cpu(ie->key_length) >
  1171. index_end))
  1172. goto err_out;
  1173. /* The last entry cannot contain a name. */
  1174. if (ie->flags & INDEX_ENTRY_END)
  1175. break;
  1176. /* Skip index root entry if continuing previous readdir. */
  1177. if (ir_pos > (u8*)ie - (u8*)ir)
  1178. continue;
  1179. /* Advance the position even if going to skip the entry. */
  1180. actor->pos = (u8*)ie - (u8*)ir;
  1181. /* Submit the name to the filldir callback. */
  1182. rc = ntfs_filldir(vol, ndir, NULL, ie, name, actor);
  1183. if (rc) {
  1184. kfree(ir);
  1185. goto abort;
  1186. }
  1187. }
  1188. /* We are done with the index root and can free the buffer. */
  1189. kfree(ir);
  1190. ir = NULL;
  1191. /* If there is no index allocation attribute we are finished. */
  1192. if (!NInoIndexAllocPresent(ndir))
  1193. goto EOD;
  1194. /* Advance fpos to the beginning of the index allocation. */
  1195. actor->pos = vol->mft_record_size;
  1196. skip_index_root:
  1197. kaddr = NULL;
  1198. prev_ia_pos = -1LL;
  1199. /* Get the offset into the index allocation attribute. */
  1200. ia_pos = (s64)actor->pos - vol->mft_record_size;
  1201. ia_mapping = vdir->i_mapping;
  1202. ntfs_debug("Inode 0x%lx, getting index bitmap.", vdir->i_ino);
  1203. bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4);
  1204. if (IS_ERR(bmp_vi)) {
  1205. ntfs_error(sb, "Failed to get bitmap attribute.");
  1206. err = PTR_ERR(bmp_vi);
  1207. goto err_out;
  1208. }
  1209. bmp_mapping = bmp_vi->i_mapping;
  1210. /* Get the starting bitmap bit position and sanity check it. */
  1211. bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
  1212. if (unlikely(bmp_pos >> 3 >= i_size_read(bmp_vi))) {
  1213. ntfs_error(sb, "Current index allocation position exceeds "
  1214. "index bitmap size.");
  1215. goto iput_err_out;
  1216. }
  1217. /* Get the starting bit position in the current bitmap page. */
  1218. cur_bmp_pos = bmp_pos & ((PAGE_SIZE * 8) - 1);
  1219. bmp_pos &= ~(u64)((PAGE_SIZE * 8) - 1);
  1220. get_next_bmp_page:
  1221. ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
  1222. (unsigned long long)bmp_pos >> (3 + PAGE_SHIFT),
  1223. (unsigned long long)bmp_pos &
  1224. (unsigned long long)((PAGE_SIZE * 8) - 1));
  1225. bmp_page = ntfs_map_page(bmp_mapping,
  1226. bmp_pos >> (3 + PAGE_SHIFT));
  1227. if (IS_ERR(bmp_page)) {
  1228. ntfs_error(sb, "Reading index bitmap failed.");
  1229. err = PTR_ERR(bmp_page);
  1230. bmp_page = NULL;
  1231. goto iput_err_out;
  1232. }
  1233. bmp = (u8*)page_address(bmp_page);
  1234. /* Find next index block in use. */
  1235. while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
  1236. find_next_index_buffer:
  1237. cur_bmp_pos++;
  1238. /*
  1239. * If we have reached the end of the bitmap page, get the next
  1240. * page, and put away the old one.
  1241. */
  1242. if (unlikely((cur_bmp_pos >> 3) >= PAGE_SIZE)) {
  1243. ntfs_unmap_page(bmp_page);
  1244. bmp_pos += PAGE_SIZE * 8;
  1245. cur_bmp_pos = 0;
  1246. goto get_next_bmp_page;
  1247. }
  1248. /* If we have reached the end of the bitmap, we are done. */
  1249. if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size))
  1250. goto unm_EOD;
  1251. ia_pos = (bmp_pos + cur_bmp_pos) <<
  1252. ndir->itype.index.block_size_bits;
  1253. }
  1254. ntfs_debug("Handling index buffer 0x%llx.",
  1255. (unsigned long long)bmp_pos + cur_bmp_pos);
  1256. /* If the current index buffer is in the same page we reuse the page. */
  1257. if ((prev_ia_pos & (s64)PAGE_MASK) !=
  1258. (ia_pos & (s64)PAGE_MASK)) {
  1259. prev_ia_pos = ia_pos;
  1260. if (likely(ia_page != NULL)) {
  1261. unlock_page(ia_page);
  1262. ntfs_unmap_page(ia_page);
  1263. }
  1264. /*
  1265. * Map the page cache page containing the current ia_pos,
  1266. * reading it from disk if necessary.
  1267. */
  1268. ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_SHIFT);
  1269. if (IS_ERR(ia_page)) {
  1270. ntfs_error(sb, "Reading index allocation data failed.");
  1271. err = PTR_ERR(ia_page);
  1272. ia_page = NULL;
  1273. goto err_out;
  1274. }
  1275. lock_page(ia_page);
  1276. kaddr = (u8*)page_address(ia_page);
  1277. }
  1278. /* Get the current index buffer. */
  1279. ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_MASK &
  1280. ~(s64)(ndir->itype.index.block_size - 1)));
  1281. /* Bounds checks. */
  1282. if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE)) {
  1283. ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
  1284. "inode 0x%lx or driver bug.", vdir->i_ino);
  1285. goto err_out;
  1286. }
  1287. /* Catch multi sector transfer fixup errors. */
  1288. if (unlikely(!ntfs_is_indx_record(ia->magic))) {
  1289. ntfs_error(sb, "Directory index record with vcn 0x%llx is "
  1290. "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
  1291. (unsigned long long)ia_pos >>
  1292. ndir->itype.index.vcn_size_bits, vdir->i_ino);
  1293. goto err_out;
  1294. }
  1295. if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
  1296. ~(s64)(ndir->itype.index.block_size - 1)) >>
  1297. ndir->itype.index.vcn_size_bits)) {
  1298. ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
  1299. "different from expected VCN (0x%llx). "
  1300. "Directory inode 0x%lx is corrupt or driver "
  1301. "bug. ", (unsigned long long)
  1302. sle64_to_cpu(ia->index_block_vcn),
  1303. (unsigned long long)ia_pos >>
  1304. ndir->itype.index.vcn_size_bits, vdir->i_ino);
  1305. goto err_out;
  1306. }
  1307. if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 !=
  1308. ndir->itype.index.block_size)) {
  1309. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  1310. "0x%lx has a size (%u) differing from the "
  1311. "directory specified size (%u). Directory "
  1312. "inode is corrupt or driver bug.",
  1313. (unsigned long long)ia_pos >>
  1314. ndir->itype.index.vcn_size_bits, vdir->i_ino,
  1315. le32_to_cpu(ia->index.allocated_size) + 0x18,
  1316. ndir->itype.index.block_size);
  1317. goto err_out;
  1318. }
  1319. index_end = (u8*)ia + ndir->itype.index.block_size;
  1320. if (unlikely(index_end > kaddr + PAGE_SIZE)) {
  1321. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  1322. "0x%lx crosses page boundary. Impossible! "
  1323. "Cannot access! This is probably a bug in the "
  1324. "driver.", (unsigned long long)ia_pos >>
  1325. ndir->itype.index.vcn_size_bits, vdir->i_ino);
  1326. goto err_out;
  1327. }
  1328. ia_start = ia_pos & ~(s64)(ndir->itype.index.block_size - 1);
  1329. index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
  1330. if (unlikely(index_end > (u8*)ia + ndir->itype.index.block_size)) {
  1331. ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
  1332. "inode 0x%lx exceeds maximum size.",
  1333. (unsigned long long)ia_pos >>
  1334. ndir->itype.index.vcn_size_bits, vdir->i_ino);
  1335. goto err_out;
  1336. }
  1337. /* The first index entry in this index buffer. */
  1338. ie = (INDEX_ENTRY*)((u8*)&ia->index +
  1339. le32_to_cpu(ia->index.entries_offset));
  1340. /*
  1341. * Loop until we exceed valid memory (corruption case) or until we
  1342. * reach the last entry or until filldir tells us it has had enough
  1343. * or signals an error (both covered by the rc test).
  1344. */
  1345. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  1346. ntfs_debug("In index allocation, offset 0x%llx.",
  1347. (unsigned long long)ia_start +
  1348. (unsigned long long)((u8*)ie - (u8*)ia));
  1349. /* Bounds checks. */
  1350. if (unlikely((u8*)ie < (u8*)ia || (u8*)ie +
  1351. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  1352. (u8*)ie + le16_to_cpu(ie->key_length) >
  1353. index_end))
  1354. goto err_out;
  1355. /* The last entry cannot contain a name. */
  1356. if (ie->flags & INDEX_ENTRY_END)
  1357. break;
  1358. /* Skip index block entry if continuing previous readdir. */
  1359. if (ia_pos - ia_start > (u8*)ie - (u8*)ia)
  1360. continue;
  1361. /* Advance the position even if going to skip the entry. */
  1362. actor->pos = (u8*)ie - (u8*)ia +
  1363. (sle64_to_cpu(ia->index_block_vcn) <<
  1364. ndir->itype.index.vcn_size_bits) +
  1365. vol->mft_record_size;
  1366. /*
  1367. * Submit the name to the @filldir callback. Note,
  1368. * ntfs_filldir() drops the lock on @ia_page but it retakes it
  1369. * before returning, unless a non-zero value is returned in
  1370. * which case the page is left unlocked.
  1371. */
  1372. rc = ntfs_filldir(vol, ndir, ia_page, ie, name, actor);
  1373. if (rc) {
  1374. /* @ia_page is already unlocked in this case. */
  1375. ntfs_unmap_page(ia_page);
  1376. ntfs_unmap_page(bmp_page);
  1377. iput(bmp_vi);
  1378. goto abort;
  1379. }
  1380. }
  1381. goto find_next_index_buffer;
  1382. unm_EOD:
  1383. if (ia_page) {
  1384. unlock_page(ia_page);
  1385. ntfs_unmap_page(ia_page);
  1386. }
  1387. ntfs_unmap_page(bmp_page);
  1388. iput(bmp_vi);
  1389. EOD:
  1390. /* We are finished, set fpos to EOD. */
  1391. actor->pos = i_size + vol->mft_record_size;
  1392. abort:
  1393. kfree(name);
  1394. return 0;
  1395. err_out:
  1396. if (bmp_page) {
  1397. ntfs_unmap_page(bmp_page);
  1398. iput_err_out:
  1399. iput(bmp_vi);
  1400. }
  1401. if (ia_page) {
  1402. unlock_page(ia_page);
  1403. ntfs_unmap_page(ia_page);
  1404. }
  1405. kfree(ir);
  1406. kfree(name);
  1407. if (ctx)
  1408. ntfs_attr_put_search_ctx(ctx);
  1409. if (m)
  1410. unmap_mft_record(ndir);
  1411. if (!err)
  1412. err = -EIO;
  1413. ntfs_debug("Failed. Returning error code %i.", -err);
  1414. return err;
  1415. }
  1416. /**
  1417. * ntfs_dir_open - called when an inode is about to be opened
  1418. * @vi: inode to be opened
  1419. * @filp: file structure describing the inode
  1420. *
  1421. * Limit directory size to the page cache limit on architectures where unsigned
  1422. * long is 32-bits. This is the most we can do for now without overflowing the
  1423. * page cache page index. Doing it this way means we don't run into problems
  1424. * because of existing too large directories. It would be better to allow the
  1425. * user to read the accessible part of the directory but I doubt very much
  1426. * anyone is going to hit this check on a 32-bit architecture, so there is no
  1427. * point in adding the extra complexity required to support this.
  1428. *
  1429. * On 64-bit architectures, the check is hopefully optimized away by the
  1430. * compiler.
  1431. */
  1432. static int ntfs_dir_open(struct inode *vi, struct file *filp)
  1433. {
  1434. if (sizeof(unsigned long) < 8) {
  1435. if (i_size_read(vi) > MAX_LFS_FILESIZE)
  1436. return -EFBIG;
  1437. }
  1438. return 0;
  1439. }
  1440. #ifdef NTFS_RW
  1441. /**
  1442. * ntfs_dir_fsync - sync a directory to disk
  1443. * @filp: directory to be synced
  1444. * @dentry: dentry describing the directory to sync
  1445. * @datasync: if non-zero only flush user data and not metadata
  1446. *
  1447. * Data integrity sync of a directory to disk. Used for fsync, fdatasync, and
  1448. * msync system calls. This function is based on file.c::ntfs_file_fsync().
  1449. *
  1450. * Write the mft record and all associated extent mft records as well as the
  1451. * $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device.
  1452. *
  1453. * If @datasync is true, we do not wait on the inode(s) to be written out
  1454. * but we always wait on the page cache pages to be written out.
  1455. *
  1456. * Note: In the past @filp could be NULL so we ignore it as we don't need it
  1457. * anyway.
  1458. *
  1459. * Locking: Caller must hold i_mutex on the inode.
  1460. *
  1461. * TODO: We should probably also write all attribute/index inodes associated
  1462. * with this inode but since we have no simple way of getting to them we ignore
  1463. * this problem for now. We do write the $BITMAP attribute if it is present
  1464. * which is the important one for a directory so things are not too bad.
  1465. */
  1466. static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
  1467. int datasync)
  1468. {
  1469. struct inode *bmp_vi, *vi = filp->f_mapping->host;
  1470. int err, ret;
  1471. ntfs_attr na;
  1472. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  1473. err = file_write_and_wait_range(filp, start, end);
  1474. if (err)
  1475. return err;
  1476. inode_lock(vi);
  1477. BUG_ON(!S_ISDIR(vi->i_mode));
  1478. /* If the bitmap attribute inode is in memory sync it, too. */
  1479. na.mft_no = vi->i_ino;
  1480. na.type = AT_BITMAP;
  1481. na.name = I30;
  1482. na.name_len = 4;
  1483. bmp_vi = ilookup5(vi->i_sb, vi->i_ino, ntfs_test_inode, &na);
  1484. if (bmp_vi) {
  1485. write_inode_now(bmp_vi, !datasync);
  1486. iput(bmp_vi);
  1487. }
  1488. ret = __ntfs_write_inode(vi, 1);
  1489. write_inode_now(vi, !datasync);
  1490. err = sync_blockdev(vi->i_sb->s_bdev);
  1491. if (unlikely(err && !ret))
  1492. ret = err;
  1493. if (likely(!ret))
  1494. ntfs_debug("Done.");
  1495. else
  1496. ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error "
  1497. "%u.", datasync ? "data" : "", vi->i_ino, -ret);
  1498. inode_unlock(vi);
  1499. return ret;
  1500. }
  1501. #endif /* NTFS_RW */
  1502. const struct file_operations ntfs_dir_ops = {
  1503. .llseek = generic_file_llseek, /* Seek inside directory. */
  1504. .read = generic_read_dir, /* Return -EISDIR. */
  1505. .iterate = ntfs_readdir, /* Read directory contents. */
  1506. #ifdef NTFS_RW
  1507. .fsync = ntfs_dir_fsync, /* Sync a directory to disk. */
  1508. #endif /* NTFS_RW */
  1509. /*.ioctl = ,*/ /* Perform function on the
  1510. mounted filesystem. */
  1511. .open = ntfs_dir_open, /* Open directory. */
  1512. };