namei.c 32 KB

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