namei.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /*
  2. * namei.c
  3. *
  4. * PURPOSE
  5. * Inode name handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1998-2004 Ben Fennema
  14. * (C) 1999-2000 Stelias Computing Inc
  15. *
  16. * HISTORY
  17. *
  18. * 12/12/98 blf Created. Split out the lookup code from dir.c
  19. * 04/19/99 blf link, mknod, symlink support
  20. */
  21. #include "udfdecl.h"
  22. #include "udf_i.h"
  23. #include "udf_sb.h"
  24. #include <linux/string.h>
  25. #include <linux/errno.h>
  26. #include <linux/mm.h>
  27. #include <linux/slab.h>
  28. #include <linux/quotaops.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/buffer_head.h>
  31. #include <linux/sched.h>
  32. static inline int udf_match(int len1, const char *name1, int len2,
  33. const char *name2)
  34. {
  35. if (len1 != len2)
  36. return 0;
  37. return !memcmp(name1, name2, len1);
  38. }
  39. int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
  40. struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
  41. uint8_t *impuse, uint8_t *fileident)
  42. {
  43. uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
  44. uint16_t crc;
  45. int offset;
  46. uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
  47. uint8_t lfi = cfi->lengthFileIdent;
  48. int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
  49. sizeof(struct fileIdentDesc);
  50. int adinicb = 0;
  51. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
  52. adinicb = 1;
  53. offset = fibh->soffset + sizeof(struct fileIdentDesc);
  54. if (impuse) {
  55. if (adinicb || (offset + liu < 0)) {
  56. memcpy((uint8_t *)sfi->impUse, impuse, liu);
  57. } else if (offset >= 0) {
  58. memcpy(fibh->ebh->b_data + offset, impuse, liu);
  59. } else {
  60. memcpy((uint8_t *)sfi->impUse, impuse, -offset);
  61. memcpy(fibh->ebh->b_data, impuse - offset,
  62. liu + offset);
  63. }
  64. }
  65. offset += liu;
  66. if (fileident) {
  67. if (adinicb || (offset + lfi < 0)) {
  68. memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
  69. } else if (offset >= 0) {
  70. memcpy(fibh->ebh->b_data + offset, fileident, lfi);
  71. } else {
  72. memcpy((uint8_t *)sfi->fileIdent + liu, fileident,
  73. -offset);
  74. memcpy(fibh->ebh->b_data, fileident - offset,
  75. lfi + offset);
  76. }
  77. }
  78. offset += lfi;
  79. if (adinicb || (offset + padlen < 0)) {
  80. memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
  81. } else if (offset >= 0) {
  82. memset(fibh->ebh->b_data + offset, 0x00, padlen);
  83. } else {
  84. memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
  85. memset(fibh->ebh->b_data, 0x00, padlen + offset);
  86. }
  87. crc = udf_crc((uint8_t *)cfi + sizeof(tag),
  88. sizeof(struct fileIdentDesc) - sizeof(tag), 0);
  89. if (fibh->sbh == fibh->ebh) {
  90. crc = udf_crc((uint8_t *)sfi->impUse,
  91. crclen + sizeof(tag) -
  92. sizeof(struct fileIdentDesc), crc);
  93. } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
  94. crc = udf_crc(fibh->ebh->b_data +
  95. sizeof(struct fileIdentDesc) +
  96. fibh->soffset,
  97. crclen + sizeof(tag) -
  98. sizeof(struct fileIdentDesc),
  99. crc);
  100. } else {
  101. crc = udf_crc((uint8_t *)sfi->impUse,
  102. -fibh->soffset - sizeof(struct fileIdentDesc),
  103. crc);
  104. crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
  105. }
  106. cfi->descTag.descCRC = cpu_to_le16(crc);
  107. cfi->descTag.descCRCLength = cpu_to_le16(crclen);
  108. cfi->descTag.tagChecksum = udf_tag_checksum(&cfi->descTag);
  109. if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
  110. memcpy((uint8_t *)sfi, (uint8_t *)cfi,
  111. sizeof(struct fileIdentDesc));
  112. } else {
  113. memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
  114. memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
  115. sizeof(struct fileIdentDesc) + fibh->soffset);
  116. }
  117. if (adinicb) {
  118. mark_inode_dirty(inode);
  119. } else {
  120. if (fibh->sbh != fibh->ebh)
  121. mark_buffer_dirty_inode(fibh->ebh, inode);
  122. mark_buffer_dirty_inode(fibh->sbh, inode);
  123. }
  124. return 0;
  125. }
  126. static struct fileIdentDesc *udf_find_entry(struct inode *dir,
  127. struct dentry *dentry,
  128. struct udf_fileident_bh *fibh,
  129. struct fileIdentDesc *cfi)
  130. {
  131. struct fileIdentDesc *fi = NULL;
  132. loff_t f_pos;
  133. int block, flen;
  134. char fname[UDF_NAME_LEN];
  135. char *nameptr;
  136. uint8_t lfi;
  137. uint16_t liu;
  138. loff_t size;
  139. kernel_lb_addr eloc;
  140. uint32_t elen;
  141. sector_t offset;
  142. struct extent_position epos = {};
  143. size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
  144. f_pos = (udf_ext0_offset(dir) >> 2);
  145. fibh->soffset = fibh->eoffset =
  146. (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
  147. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  148. fibh->sbh = fibh->ebh = NULL;
  149. else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
  150. &epos, &eloc, &elen, &offset) ==
  151. (EXT_RECORDED_ALLOCATED >> 30)) {
  152. block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
  153. if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
  154. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
  155. epos.offset -= sizeof(short_ad);
  156. else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
  157. epos.offset -= sizeof(long_ad);
  158. } else
  159. offset = 0;
  160. fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
  161. if (!fibh->sbh) {
  162. brelse(epos.bh);
  163. return NULL;
  164. }
  165. } else {
  166. brelse(epos.bh);
  167. return NULL;
  168. }
  169. while ((f_pos < size)) {
  170. fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
  171. &elen, &offset);
  172. if (!fi) {
  173. if (fibh->sbh != fibh->ebh)
  174. brelse(fibh->ebh);
  175. brelse(fibh->sbh);
  176. brelse(epos.bh);
  177. return NULL;
  178. }
  179. liu = le16_to_cpu(cfi->lengthOfImpUse);
  180. lfi = cfi->lengthFileIdent;
  181. if (fibh->sbh == fibh->ebh) {
  182. nameptr = fi->fileIdent + liu;
  183. } else {
  184. int poffset; /* Unpaded ending offset */
  185. poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
  186. liu + lfi;
  187. if (poffset >= lfi)
  188. nameptr = (uint8_t *)(fibh->ebh->b_data +
  189. poffset - lfi);
  190. else {
  191. nameptr = fname;
  192. memcpy(nameptr, fi->fileIdent + liu,
  193. lfi - poffset);
  194. memcpy(nameptr + lfi - poffset,
  195. fibh->ebh->b_data, poffset);
  196. }
  197. }
  198. if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
  199. if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
  200. continue;
  201. }
  202. if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
  203. if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
  204. continue;
  205. }
  206. if (!lfi)
  207. continue;
  208. flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
  209. if (flen && udf_match(flen, fname, dentry->d_name.len,
  210. dentry->d_name.name)) {
  211. brelse(epos.bh);
  212. return fi;
  213. }
  214. }
  215. if (fibh->sbh != fibh->ebh)
  216. brelse(fibh->ebh);
  217. brelse(fibh->sbh);
  218. brelse(epos.bh);
  219. return NULL;
  220. }
  221. /*
  222. * udf_lookup
  223. *
  224. * PURPOSE
  225. * Look-up the inode for a given name.
  226. *
  227. * DESCRIPTION
  228. * Required - lookup_dentry() will return -ENOTDIR if this routine is not
  229. * available for a directory. The filesystem is useless if this routine is
  230. * not available for at least the filesystem's root directory.
  231. *
  232. * This routine is passed an incomplete dentry - it must be completed by
  233. * calling d_add(dentry, inode). If the name does not exist, then the
  234. * specified inode must be set to null. An error should only be returned
  235. * when the lookup fails for a reason other than the name not existing.
  236. * Note that the directory inode semaphore is held during the call.
  237. *
  238. * Refer to lookup_dentry() in fs/namei.c
  239. * lookup_dentry() -> lookup() -> real_lookup() -> .
  240. *
  241. * PRE-CONDITIONS
  242. * dir Pointer to inode of parent directory.
  243. * dentry Pointer to dentry to complete.
  244. * nd Pointer to lookup nameidata
  245. *
  246. * POST-CONDITIONS
  247. * <return> Zero on success.
  248. *
  249. * HISTORY
  250. * July 1, 1997 - Andrew E. Mileski
  251. * Written, tested, and released.
  252. */
  253. static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
  254. struct nameidata *nd)
  255. {
  256. struct inode *inode = NULL;
  257. struct fileIdentDesc cfi;
  258. struct udf_fileident_bh fibh;
  259. if (dentry->d_name.len > UDF_NAME_LEN - 2)
  260. return ERR_PTR(-ENAMETOOLONG);
  261. lock_kernel();
  262. #ifdef UDF_RECOVERY
  263. /* temporary shorthand for specifying files by inode number */
  264. if (!strncmp(dentry->d_name.name, ".B=", 3)) {
  265. kernel_lb_addr lb = {
  266. .logicalBlockNum = 0,
  267. .partitionReferenceNum =
  268. simple_strtoul(dentry->d_name.name + 3,
  269. NULL, 0),
  270. };
  271. inode = udf_iget(dir->i_sb, lb);
  272. if (!inode) {
  273. unlock_kernel();
  274. return ERR_PTR(-EACCES);
  275. }
  276. } else
  277. #endif /* UDF_RECOVERY */
  278. if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
  279. if (fibh.sbh != fibh.ebh)
  280. brelse(fibh.ebh);
  281. brelse(fibh.sbh);
  282. inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
  283. if (!inode) {
  284. unlock_kernel();
  285. return ERR_PTR(-EACCES);
  286. }
  287. }
  288. unlock_kernel();
  289. d_add(dentry, inode);
  290. return NULL;
  291. }
  292. static struct fileIdentDesc *udf_add_entry(struct inode *dir,
  293. struct dentry *dentry,
  294. struct udf_fileident_bh *fibh,
  295. struct fileIdentDesc *cfi, int *err)
  296. {
  297. struct super_block *sb = dir->i_sb;
  298. struct fileIdentDesc *fi = NULL;
  299. char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
  300. int namelen;
  301. loff_t f_pos;
  302. int flen;
  303. char *nameptr;
  304. loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
  305. int nfidlen;
  306. uint8_t lfi;
  307. uint16_t liu;
  308. int block;
  309. kernel_lb_addr eloc;
  310. uint32_t elen;
  311. sector_t offset;
  312. struct extent_position epos = {};
  313. if (dentry) {
  314. if (!dentry->d_name.len) {
  315. *err = -EINVAL;
  316. return NULL;
  317. }
  318. namelen = udf_put_filename(sb, dentry->d_name.name, name,
  319. dentry->d_name.len);
  320. if (!namelen) {
  321. *err = -ENAMETOOLONG;
  322. return NULL;
  323. }
  324. } else {
  325. namelen = 0;
  326. }
  327. nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
  328. f_pos = (udf_ext0_offset(dir) >> 2);
  329. fibh->soffset = fibh->eoffset =
  330. (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
  331. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  332. fibh->sbh = fibh->ebh = NULL;
  333. else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
  334. &epos, &eloc, &elen, &offset) ==
  335. (EXT_RECORDED_ALLOCATED >> 30)) {
  336. block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
  337. if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
  338. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
  339. epos.offset -= sizeof(short_ad);
  340. else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
  341. epos.offset -= sizeof(long_ad);
  342. } else
  343. offset = 0;
  344. fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
  345. if (!fibh->sbh) {
  346. brelse(epos.bh);
  347. *err = -EIO;
  348. return NULL;
  349. }
  350. block = UDF_I_LOCATION(dir).logicalBlockNum;
  351. } else {
  352. block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
  353. fibh->sbh = fibh->ebh = NULL;
  354. fibh->soffset = fibh->eoffset = sb->s_blocksize;
  355. goto add;
  356. }
  357. while ((f_pos < size)) {
  358. fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
  359. &elen, &offset);
  360. if (!fi) {
  361. if (fibh->sbh != fibh->ebh)
  362. brelse(fibh->ebh);
  363. brelse(fibh->sbh);
  364. brelse(epos.bh);
  365. *err = -EIO;
  366. return NULL;
  367. }
  368. liu = le16_to_cpu(cfi->lengthOfImpUse);
  369. lfi = cfi->lengthFileIdent;
  370. if (fibh->sbh == fibh->ebh)
  371. nameptr = fi->fileIdent + liu;
  372. else {
  373. int poffset; /* Unpaded ending offset */
  374. poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
  375. liu + lfi;
  376. if (poffset >= lfi)
  377. nameptr = (char *)(fibh->ebh->b_data +
  378. poffset - lfi);
  379. else {
  380. nameptr = fname;
  381. memcpy(nameptr, fi->fileIdent + liu,
  382. lfi - poffset);
  383. memcpy(nameptr + lfi - poffset,
  384. fibh->ebh->b_data, poffset);
  385. }
  386. }
  387. if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
  388. if (((sizeof(struct fileIdentDesc) +
  389. liu + lfi + 3) & ~3) == nfidlen) {
  390. brelse(epos.bh);
  391. cfi->descTag.tagSerialNum = cpu_to_le16(1);
  392. cfi->fileVersionNum = cpu_to_le16(1);
  393. cfi->fileCharacteristics = 0;
  394. cfi->lengthFileIdent = namelen;
  395. cfi->lengthOfImpUse = cpu_to_le16(0);
  396. if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
  397. name))
  398. return fi;
  399. else {
  400. *err = -EIO;
  401. return NULL;
  402. }
  403. }
  404. }
  405. if (!lfi || !dentry)
  406. continue;
  407. flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
  408. if (flen && udf_match(flen, fname, dentry->d_name.len,
  409. dentry->d_name.name)) {
  410. if (fibh->sbh != fibh->ebh)
  411. brelse(fibh->ebh);
  412. brelse(fibh->sbh);
  413. brelse(epos.bh);
  414. *err = -EEXIST;
  415. return NULL;
  416. }
  417. }
  418. add:
  419. f_pos += nfidlen;
  420. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
  421. sb->s_blocksize - fibh->eoffset < nfidlen) {
  422. brelse(epos.bh);
  423. epos.bh = NULL;
  424. fibh->soffset -= udf_ext0_offset(dir);
  425. fibh->eoffset -= udf_ext0_offset(dir);
  426. f_pos -= (udf_ext0_offset(dir) >> 2);
  427. if (fibh->sbh != fibh->ebh)
  428. brelse(fibh->ebh);
  429. brelse(fibh->sbh);
  430. fibh->sbh = fibh->ebh =
  431. udf_expand_dir_adinicb(dir, &block, err);
  432. if (!fibh->sbh)
  433. return NULL;
  434. epos.block = UDF_I_LOCATION(dir);
  435. eloc.logicalBlockNum = block;
  436. eloc.partitionReferenceNum =
  437. UDF_I_LOCATION(dir).partitionReferenceNum;
  438. elen = dir->i_sb->s_blocksize;
  439. epos.offset = udf_file_entry_alloc_offset(dir);
  440. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
  441. epos.offset += sizeof(short_ad);
  442. else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
  443. epos.offset += sizeof(long_ad);
  444. }
  445. if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
  446. fibh->soffset = fibh->eoffset;
  447. fibh->eoffset += nfidlen;
  448. if (fibh->sbh != fibh->ebh) {
  449. brelse(fibh->sbh);
  450. fibh->sbh = fibh->ebh;
  451. }
  452. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
  453. block = UDF_I_LOCATION(dir).logicalBlockNum;
  454. fi = (struct fileIdentDesc *)
  455. (UDF_I_DATA(dir) + fibh->soffset -
  456. udf_ext0_offset(dir) +
  457. UDF_I_LENEATTR(dir));
  458. } else {
  459. block = eloc.logicalBlockNum +
  460. ((elen - 1) >>
  461. dir->i_sb->s_blocksize_bits);
  462. fi = (struct fileIdentDesc *)
  463. (fibh->sbh->b_data + fibh->soffset);
  464. }
  465. } else {
  466. fibh->soffset = fibh->eoffset - sb->s_blocksize;
  467. fibh->eoffset += nfidlen - sb->s_blocksize;
  468. if (fibh->sbh != fibh->ebh) {
  469. brelse(fibh->sbh);
  470. fibh->sbh = fibh->ebh;
  471. }
  472. block = eloc.logicalBlockNum + ((elen - 1) >>
  473. dir->i_sb->s_blocksize_bits);
  474. fibh->ebh = udf_bread(dir,
  475. f_pos >> (dir->i_sb->s_blocksize_bits - 2),
  476. 1, err);
  477. if (!fibh->ebh) {
  478. brelse(epos.bh);
  479. brelse(fibh->sbh);
  480. return NULL;
  481. }
  482. if (!fibh->soffset) {
  483. if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
  484. (EXT_RECORDED_ALLOCATED >> 30)) {
  485. block = eloc.logicalBlockNum + ((elen - 1) >>
  486. dir->i_sb->s_blocksize_bits);
  487. } else
  488. block++;
  489. brelse(fibh->sbh);
  490. fibh->sbh = fibh->ebh;
  491. fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
  492. } else {
  493. fi = (struct fileIdentDesc *)
  494. (fibh->sbh->b_data + sb->s_blocksize +
  495. fibh->soffset);
  496. }
  497. }
  498. memset(cfi, 0, sizeof(struct fileIdentDesc));
  499. if (UDF_SB(sb)->s_udfrev >= 0x0200)
  500. udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
  501. sizeof(tag));
  502. else
  503. udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
  504. sizeof(tag));
  505. cfi->fileVersionNum = cpu_to_le16(1);
  506. cfi->lengthFileIdent = namelen;
  507. cfi->lengthOfImpUse = cpu_to_le16(0);
  508. if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
  509. brelse(epos.bh);
  510. dir->i_size += nfidlen;
  511. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  512. UDF_I_LENALLOC(dir) += nfidlen;
  513. mark_inode_dirty(dir);
  514. return fi;
  515. } else {
  516. brelse(epos.bh);
  517. if (fibh->sbh != fibh->ebh)
  518. brelse(fibh->ebh);
  519. brelse(fibh->sbh);
  520. *err = -EIO;
  521. return NULL;
  522. }
  523. }
  524. static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
  525. struct udf_fileident_bh *fibh,
  526. struct fileIdentDesc *cfi)
  527. {
  528. cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
  529. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
  530. memset(&(cfi->icb), 0x00, sizeof(long_ad));
  531. return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
  532. }
  533. static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
  534. struct nameidata *nd)
  535. {
  536. struct udf_fileident_bh fibh;
  537. struct inode *inode;
  538. struct fileIdentDesc cfi, *fi;
  539. int err;
  540. lock_kernel();
  541. inode = udf_new_inode(dir, mode, &err);
  542. if (!inode) {
  543. unlock_kernel();
  544. return err;
  545. }
  546. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
  547. inode->i_data.a_ops = &udf_adinicb_aops;
  548. else
  549. inode->i_data.a_ops = &udf_aops;
  550. inode->i_op = &udf_file_inode_operations;
  551. inode->i_fop = &udf_file_operations;
  552. inode->i_mode = mode;
  553. mark_inode_dirty(inode);
  554. fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
  555. if (!fi) {
  556. inode->i_nlink--;
  557. mark_inode_dirty(inode);
  558. iput(inode);
  559. unlock_kernel();
  560. return err;
  561. }
  562. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  563. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
  564. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  565. cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
  566. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  567. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  568. mark_inode_dirty(dir);
  569. if (fibh.sbh != fibh.ebh)
  570. brelse(fibh.ebh);
  571. brelse(fibh.sbh);
  572. unlock_kernel();
  573. d_instantiate(dentry, inode);
  574. return 0;
  575. }
  576. static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
  577. dev_t rdev)
  578. {
  579. struct inode *inode;
  580. struct udf_fileident_bh fibh;
  581. struct fileIdentDesc cfi, *fi;
  582. int err;
  583. if (!old_valid_dev(rdev))
  584. return -EINVAL;
  585. lock_kernel();
  586. err = -EIO;
  587. inode = udf_new_inode(dir, mode, &err);
  588. if (!inode)
  589. goto out;
  590. inode->i_uid = current->fsuid;
  591. init_special_inode(inode, mode, rdev);
  592. fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
  593. if (!fi) {
  594. inode->i_nlink--;
  595. mark_inode_dirty(inode);
  596. iput(inode);
  597. unlock_kernel();
  598. return err;
  599. }
  600. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  601. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
  602. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  603. cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
  604. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  605. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  606. mark_inode_dirty(dir);
  607. mark_inode_dirty(inode);
  608. if (fibh.sbh != fibh.ebh)
  609. brelse(fibh.ebh);
  610. brelse(fibh.sbh);
  611. d_instantiate(dentry, inode);
  612. err = 0;
  613. out:
  614. unlock_kernel();
  615. return err;
  616. }
  617. static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  618. {
  619. struct inode *inode;
  620. struct udf_fileident_bh fibh;
  621. struct fileIdentDesc cfi, *fi;
  622. int err;
  623. lock_kernel();
  624. err = -EMLINK;
  625. if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
  626. goto out;
  627. err = -EIO;
  628. inode = udf_new_inode(dir, S_IFDIR, &err);
  629. if (!inode)
  630. goto out;
  631. inode->i_op = &udf_dir_inode_operations;
  632. inode->i_fop = &udf_dir_operations;
  633. fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
  634. if (!fi) {
  635. inode->i_nlink--;
  636. mark_inode_dirty(inode);
  637. iput(inode);
  638. goto out;
  639. }
  640. inode->i_nlink = 2;
  641. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  642. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
  643. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  644. cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
  645. cfi.fileCharacteristics =
  646. FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
  647. udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
  648. brelse(fibh.sbh);
  649. inode->i_mode = S_IFDIR | mode;
  650. if (dir->i_mode & S_ISGID)
  651. inode->i_mode |= S_ISGID;
  652. mark_inode_dirty(inode);
  653. fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
  654. if (!fi) {
  655. inode->i_nlink = 0;
  656. mark_inode_dirty(inode);
  657. iput(inode);
  658. goto out;
  659. }
  660. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  661. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
  662. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  663. cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
  664. cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
  665. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  666. inc_nlink(dir);
  667. mark_inode_dirty(dir);
  668. d_instantiate(dentry, inode);
  669. if (fibh.sbh != fibh.ebh)
  670. brelse(fibh.ebh);
  671. brelse(fibh.sbh);
  672. err = 0;
  673. out:
  674. unlock_kernel();
  675. return err;
  676. }
  677. static int empty_dir(struct inode *dir)
  678. {
  679. struct fileIdentDesc *fi, cfi;
  680. struct udf_fileident_bh fibh;
  681. loff_t f_pos;
  682. loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
  683. int block;
  684. kernel_lb_addr eloc;
  685. uint32_t elen;
  686. sector_t offset;
  687. struct extent_position epos = {};
  688. f_pos = (udf_ext0_offset(dir) >> 2);
  689. fibh.soffset = fibh.eoffset =
  690. (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
  691. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  692. fibh.sbh = fibh.ebh = NULL;
  693. else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
  694. &epos, &eloc, &elen, &offset) ==
  695. (EXT_RECORDED_ALLOCATED >> 30)) {
  696. block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
  697. if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
  698. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
  699. epos.offset -= sizeof(short_ad);
  700. else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
  701. epos.offset -= sizeof(long_ad);
  702. } else
  703. offset = 0;
  704. fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
  705. if (!fibh.sbh) {
  706. brelse(epos.bh);
  707. return 0;
  708. }
  709. } else {
  710. brelse(epos.bh);
  711. return 0;
  712. }
  713. while ((f_pos < size)) {
  714. fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
  715. &elen, &offset);
  716. if (!fi) {
  717. if (fibh.sbh != fibh.ebh)
  718. brelse(fibh.ebh);
  719. brelse(fibh.sbh);
  720. brelse(epos.bh);
  721. return 0;
  722. }
  723. if (cfi.lengthFileIdent &&
  724. (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
  725. if (fibh.sbh != fibh.ebh)
  726. brelse(fibh.ebh);
  727. brelse(fibh.sbh);
  728. brelse(epos.bh);
  729. return 0;
  730. }
  731. }
  732. if (fibh.sbh != fibh.ebh)
  733. brelse(fibh.ebh);
  734. brelse(fibh.sbh);
  735. brelse(epos.bh);
  736. return 1;
  737. }
  738. static int udf_rmdir(struct inode *dir, struct dentry *dentry)
  739. {
  740. int retval;
  741. struct inode *inode = dentry->d_inode;
  742. struct udf_fileident_bh fibh;
  743. struct fileIdentDesc *fi, cfi;
  744. kernel_lb_addr tloc;
  745. retval = -ENOENT;
  746. lock_kernel();
  747. fi = udf_find_entry(dir, dentry, &fibh, &cfi);
  748. if (!fi)
  749. goto out;
  750. retval = -EIO;
  751. tloc = lelb_to_cpu(cfi.icb.extLocation);
  752. if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
  753. goto end_rmdir;
  754. retval = -ENOTEMPTY;
  755. if (!empty_dir(inode))
  756. goto end_rmdir;
  757. retval = udf_delete_entry(dir, fi, &fibh, &cfi);
  758. if (retval)
  759. goto end_rmdir;
  760. if (inode->i_nlink != 2)
  761. udf_warning(inode->i_sb, "udf_rmdir",
  762. "empty directory has nlink != 2 (%d)",
  763. inode->i_nlink);
  764. clear_nlink(inode);
  765. inode->i_size = 0;
  766. inode_dec_link_count(dir);
  767. inode->i_ctime = dir->i_ctime = dir->i_mtime =
  768. current_fs_time(dir->i_sb);
  769. mark_inode_dirty(dir);
  770. end_rmdir:
  771. if (fibh.sbh != fibh.ebh)
  772. brelse(fibh.ebh);
  773. brelse(fibh.sbh);
  774. out:
  775. unlock_kernel();
  776. return retval;
  777. }
  778. static int udf_unlink(struct inode *dir, struct dentry *dentry)
  779. {
  780. int retval;
  781. struct inode *inode = dentry->d_inode;
  782. struct udf_fileident_bh fibh;
  783. struct fileIdentDesc *fi;
  784. struct fileIdentDesc cfi;
  785. kernel_lb_addr tloc;
  786. retval = -ENOENT;
  787. lock_kernel();
  788. fi = udf_find_entry(dir, dentry, &fibh, &cfi);
  789. if (!fi)
  790. goto out;
  791. retval = -EIO;
  792. tloc = lelb_to_cpu(cfi.icb.extLocation);
  793. if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
  794. goto end_unlink;
  795. if (!inode->i_nlink) {
  796. udf_debug("Deleting nonexistent file (%lu), %d\n",
  797. inode->i_ino, inode->i_nlink);
  798. inode->i_nlink = 1;
  799. }
  800. retval = udf_delete_entry(dir, fi, &fibh, &cfi);
  801. if (retval)
  802. goto end_unlink;
  803. dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
  804. mark_inode_dirty(dir);
  805. inode_dec_link_count(inode);
  806. inode->i_ctime = dir->i_ctime;
  807. retval = 0;
  808. end_unlink:
  809. if (fibh.sbh != fibh.ebh)
  810. brelse(fibh.ebh);
  811. brelse(fibh.sbh);
  812. out:
  813. unlock_kernel();
  814. return retval;
  815. }
  816. static int udf_symlink(struct inode *dir, struct dentry *dentry,
  817. const char *symname)
  818. {
  819. struct inode *inode;
  820. struct pathComponent *pc;
  821. char *compstart;
  822. struct udf_fileident_bh fibh;
  823. struct extent_position epos = {};
  824. int eoffset, elen = 0;
  825. struct fileIdentDesc *fi;
  826. struct fileIdentDesc cfi;
  827. char *ea;
  828. int err;
  829. int block;
  830. char name[UDF_NAME_LEN];
  831. int namelen;
  832. struct buffer_head *bh;
  833. lock_kernel();
  834. inode = udf_new_inode(dir, S_IFLNK, &err);
  835. if (!inode)
  836. goto out;
  837. inode->i_mode = S_IFLNK | S_IRWXUGO;
  838. inode->i_data.a_ops = &udf_symlink_aops;
  839. inode->i_op = &page_symlink_inode_operations;
  840. if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB) {
  841. kernel_lb_addr eloc;
  842. uint32_t elen;
  843. block = udf_new_block(inode->i_sb, inode,
  844. UDF_I_LOCATION(inode).partitionReferenceNum,
  845. UDF_I_LOCATION(inode).logicalBlockNum, &err);
  846. if (!block)
  847. goto out_no_entry;
  848. epos.block = UDF_I_LOCATION(inode);
  849. epos.offset = udf_file_entry_alloc_offset(inode);
  850. epos.bh = NULL;
  851. eloc.logicalBlockNum = block;
  852. eloc.partitionReferenceNum =
  853. UDF_I_LOCATION(inode).partitionReferenceNum;
  854. elen = inode->i_sb->s_blocksize;
  855. UDF_I_LENEXTENTS(inode) = elen;
  856. udf_add_aext(inode, &epos, eloc, elen, 0);
  857. brelse(epos.bh);
  858. block = udf_get_pblock(inode->i_sb, block,
  859. UDF_I_LOCATION(inode).partitionReferenceNum,
  860. 0);
  861. epos.bh = udf_tread(inode->i_sb, block);
  862. lock_buffer(epos.bh);
  863. memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
  864. set_buffer_uptodate(epos.bh);
  865. unlock_buffer(epos.bh);
  866. mark_buffer_dirty_inode(epos.bh, inode);
  867. ea = epos.bh->b_data + udf_ext0_offset(inode);
  868. } else {
  869. ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
  870. }
  871. eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
  872. pc = (struct pathComponent *)ea;
  873. if (*symname == '/') {
  874. do {
  875. symname++;
  876. } while (*symname == '/');
  877. pc->componentType = 1;
  878. pc->lengthComponentIdent = 0;
  879. pc->componentFileVersionNum = 0;
  880. pc += sizeof(struct pathComponent);
  881. elen += sizeof(struct pathComponent);
  882. }
  883. err = -ENAMETOOLONG;
  884. while (*symname) {
  885. if (elen + sizeof(struct pathComponent) > eoffset)
  886. goto out_no_entry;
  887. pc = (struct pathComponent *)(ea + elen);
  888. compstart = (char *)symname;
  889. do {
  890. symname++;
  891. } while (*symname && *symname != '/');
  892. pc->componentType = 5;
  893. pc->lengthComponentIdent = 0;
  894. pc->componentFileVersionNum = 0;
  895. if (compstart[0] == '.') {
  896. if ((symname - compstart) == 1)
  897. pc->componentType = 4;
  898. else if ((symname - compstart) == 2 &&
  899. compstart[1] == '.')
  900. pc->componentType = 3;
  901. }
  902. if (pc->componentType == 5) {
  903. namelen = udf_put_filename(inode->i_sb, compstart, name,
  904. symname - compstart);
  905. if (!namelen)
  906. goto out_no_entry;
  907. if (elen + sizeof(struct pathComponent) + namelen >
  908. eoffset)
  909. goto out_no_entry;
  910. else
  911. pc->lengthComponentIdent = namelen;
  912. memcpy(pc->componentIdent, name, namelen);
  913. }
  914. elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
  915. if (*symname) {
  916. do {
  917. symname++;
  918. } while (*symname == '/');
  919. }
  920. }
  921. brelse(epos.bh);
  922. inode->i_size = elen;
  923. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
  924. UDF_I_LENALLOC(inode) = inode->i_size;
  925. mark_inode_dirty(inode);
  926. fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
  927. if (!fi)
  928. goto out_no_entry;
  929. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  930. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
  931. bh = UDF_SB(inode->i_sb)->s_lvid_bh;
  932. if (bh) {
  933. struct logicalVolIntegrityDesc *lvid =
  934. (struct logicalVolIntegrityDesc *)bh->b_data;
  935. struct logicalVolHeaderDesc *lvhd;
  936. uint64_t uniqueID;
  937. lvhd = (struct logicalVolHeaderDesc *)
  938. lvid->logicalVolContentsUse;
  939. uniqueID = le64_to_cpu(lvhd->uniqueID);
  940. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  941. cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
  942. if (!(++uniqueID & 0x00000000FFFFFFFFUL))
  943. uniqueID += 16;
  944. lvhd->uniqueID = cpu_to_le64(uniqueID);
  945. mark_buffer_dirty(bh);
  946. }
  947. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  948. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  949. mark_inode_dirty(dir);
  950. if (fibh.sbh != fibh.ebh)
  951. brelse(fibh.ebh);
  952. brelse(fibh.sbh);
  953. d_instantiate(dentry, inode);
  954. err = 0;
  955. out:
  956. unlock_kernel();
  957. return err;
  958. out_no_entry:
  959. inode_dec_link_count(inode);
  960. iput(inode);
  961. goto out;
  962. }
  963. static int udf_link(struct dentry *old_dentry, struct inode *dir,
  964. struct dentry *dentry)
  965. {
  966. struct inode *inode = old_dentry->d_inode;
  967. struct udf_fileident_bh fibh;
  968. struct fileIdentDesc cfi, *fi;
  969. int err;
  970. struct buffer_head *bh;
  971. lock_kernel();
  972. if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
  973. unlock_kernel();
  974. return -EMLINK;
  975. }
  976. fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
  977. if (!fi) {
  978. unlock_kernel();
  979. return err;
  980. }
  981. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  982. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
  983. bh = UDF_SB(inode->i_sb)->s_lvid_bh;
  984. if (bh) {
  985. struct logicalVolIntegrityDesc *lvid =
  986. (struct logicalVolIntegrityDesc *)bh->b_data;
  987. struct logicalVolHeaderDesc *lvhd;
  988. uint64_t uniqueID;
  989. lvhd = (struct logicalVolHeaderDesc *)
  990. (lvid->logicalVolContentsUse);
  991. uniqueID = le64_to_cpu(lvhd->uniqueID);
  992. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  993. cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
  994. if (!(++uniqueID & 0x00000000FFFFFFFFUL))
  995. uniqueID += 16;
  996. lvhd->uniqueID = cpu_to_le64(uniqueID);
  997. mark_buffer_dirty(bh);
  998. }
  999. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  1000. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  1001. mark_inode_dirty(dir);
  1002. if (fibh.sbh != fibh.ebh)
  1003. brelse(fibh.ebh);
  1004. brelse(fibh.sbh);
  1005. inc_nlink(inode);
  1006. inode->i_ctime = current_fs_time(inode->i_sb);
  1007. mark_inode_dirty(inode);
  1008. atomic_inc(&inode->i_count);
  1009. d_instantiate(dentry, inode);
  1010. unlock_kernel();
  1011. return 0;
  1012. }
  1013. /* Anybody can rename anything with this: the permission checks are left to the
  1014. * higher-level routines.
  1015. */
  1016. static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
  1017. struct inode *new_dir, struct dentry *new_dentry)
  1018. {
  1019. struct inode *old_inode = old_dentry->d_inode;
  1020. struct inode *new_inode = new_dentry->d_inode;
  1021. struct udf_fileident_bh ofibh, nfibh;
  1022. struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
  1023. struct fileIdentDesc ocfi, ncfi;
  1024. struct buffer_head *dir_bh = NULL;
  1025. int retval = -ENOENT;
  1026. kernel_lb_addr tloc;
  1027. lock_kernel();
  1028. ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
  1029. if (ofi) {
  1030. if (ofibh.sbh != ofibh.ebh)
  1031. brelse(ofibh.ebh);
  1032. brelse(ofibh.sbh);
  1033. }
  1034. tloc = lelb_to_cpu(ocfi.icb.extLocation);
  1035. if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
  1036. != old_inode->i_ino)
  1037. goto end_rename;
  1038. nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
  1039. if (nfi) {
  1040. if (!new_inode) {
  1041. if (nfibh.sbh != nfibh.ebh)
  1042. brelse(nfibh.ebh);
  1043. brelse(nfibh.sbh);
  1044. nfi = NULL;
  1045. }
  1046. }
  1047. if (S_ISDIR(old_inode->i_mode)) {
  1048. uint32_t offset = udf_ext0_offset(old_inode);
  1049. if (new_inode) {
  1050. retval = -ENOTEMPTY;
  1051. if (!empty_dir(new_inode))
  1052. goto end_rename;
  1053. }
  1054. retval = -EIO;
  1055. if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
  1056. dir_fi = udf_get_fileident(
  1057. UDF_I_DATA(old_inode) -
  1058. (UDF_I_EFE(old_inode) ?
  1059. sizeof(struct extendedFileEntry) :
  1060. sizeof(struct fileEntry)),
  1061. old_inode->i_sb->s_blocksize, &offset);
  1062. } else {
  1063. dir_bh = udf_bread(old_inode, 0, 0, &retval);
  1064. if (!dir_bh)
  1065. goto end_rename;
  1066. dir_fi = udf_get_fileident(dir_bh->b_data,
  1067. old_inode->i_sb->s_blocksize, &offset);
  1068. }
  1069. if (!dir_fi)
  1070. goto end_rename;
  1071. tloc = lelb_to_cpu(dir_fi->icb.extLocation);
  1072. if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
  1073. old_dir->i_ino)
  1074. goto end_rename;
  1075. retval = -EMLINK;
  1076. if (!new_inode &&
  1077. new_dir->i_nlink >=
  1078. (256 << sizeof(new_dir->i_nlink)) - 1)
  1079. goto end_rename;
  1080. }
  1081. if (!nfi) {
  1082. nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
  1083. &retval);
  1084. if (!nfi)
  1085. goto end_rename;
  1086. }
  1087. /*
  1088. * Like most other Unix systems, set the ctime for inodes on a
  1089. * rename.
  1090. */
  1091. old_inode->i_ctime = current_fs_time(old_inode->i_sb);
  1092. mark_inode_dirty(old_inode);
  1093. /*
  1094. * ok, that's it
  1095. */
  1096. ncfi.fileVersionNum = ocfi.fileVersionNum;
  1097. ncfi.fileCharacteristics = ocfi.fileCharacteristics;
  1098. memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
  1099. udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
  1100. /* The old fid may have moved - find it again */
  1101. ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
  1102. udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
  1103. if (new_inode) {
  1104. new_inode->i_ctime = current_fs_time(new_inode->i_sb);
  1105. inode_dec_link_count(new_inode);
  1106. }
  1107. old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
  1108. mark_inode_dirty(old_dir);
  1109. if (dir_fi) {
  1110. dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
  1111. udf_update_tag((char *)dir_fi,
  1112. (sizeof(struct fileIdentDesc) +
  1113. le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
  1114. if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
  1115. mark_inode_dirty(old_inode);
  1116. else
  1117. mark_buffer_dirty_inode(dir_bh, old_inode);
  1118. inode_dec_link_count(old_dir);
  1119. if (new_inode)
  1120. inode_dec_link_count(new_inode);
  1121. else {
  1122. inc_nlink(new_dir);
  1123. mark_inode_dirty(new_dir);
  1124. }
  1125. }
  1126. if (ofi) {
  1127. if (ofibh.sbh != ofibh.ebh)
  1128. brelse(ofibh.ebh);
  1129. brelse(ofibh.sbh);
  1130. }
  1131. retval = 0;
  1132. end_rename:
  1133. brelse(dir_bh);
  1134. if (nfi) {
  1135. if (nfibh.sbh != nfibh.ebh)
  1136. brelse(nfibh.ebh);
  1137. brelse(nfibh.sbh);
  1138. }
  1139. unlock_kernel();
  1140. return retval;
  1141. }
  1142. const struct inode_operations udf_dir_inode_operations = {
  1143. .lookup = udf_lookup,
  1144. .create = udf_create,
  1145. .link = udf_link,
  1146. .unlink = udf_unlink,
  1147. .symlink = udf_symlink,
  1148. .mkdir = udf_mkdir,
  1149. .rmdir = udf_rmdir,
  1150. .mknod = udf_mknod,
  1151. .rename = udf_rename,
  1152. };