dir.c 52 KB

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