namei.c 32 KB

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