namei.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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 = dir->i_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. if (dentry) {
  304. if (!dentry->d_name.len) {
  305. *err = -EINVAL;
  306. return NULL;
  307. }
  308. if (!(namelen = udf_put_filename(sb, dentry->d_name.name, name,
  309. dentry->d_name.len))) {
  310. *err = -ENAMETOOLONG;
  311. return NULL;
  312. }
  313. } else {
  314. namelen = 0;
  315. }
  316. nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
  317. f_pos = (udf_ext0_offset(dir) >> 2);
  318. fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
  319. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
  320. fibh->sbh = fibh->ebh = NULL;
  321. } else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
  322. &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
  323. block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
  324. if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
  325. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
  326. epos.offset -= sizeof(short_ad);
  327. else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
  328. epos.offset -= sizeof(long_ad);
  329. } else {
  330. offset = 0;
  331. }
  332. if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
  333. brelse(epos.bh);
  334. *err = -EIO;
  335. return NULL;
  336. }
  337. block = UDF_I_LOCATION(dir).logicalBlockNum;
  338. } else {
  339. block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
  340. fibh->sbh = fibh->ebh = NULL;
  341. fibh->soffset = fibh->eoffset = sb->s_blocksize;
  342. goto add;
  343. }
  344. while ((f_pos < size)) {
  345. fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
  346. &elen, &offset);
  347. if (!fi) {
  348. if (fibh->sbh != fibh->ebh)
  349. brelse(fibh->ebh);
  350. brelse(fibh->sbh);
  351. brelse(epos.bh);
  352. *err = -EIO;
  353. return NULL;
  354. }
  355. liu = le16_to_cpu(cfi->lengthOfImpUse);
  356. lfi = cfi->lengthFileIdent;
  357. if (fibh->sbh == fibh->ebh) {
  358. nameptr = fi->fileIdent + liu;
  359. } else {
  360. int poffset; /* Unpaded ending offset */
  361. poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
  362. if (poffset >= lfi) {
  363. nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
  364. } else {
  365. nameptr = fname;
  366. memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
  367. memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
  368. }
  369. }
  370. if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
  371. if (((sizeof(struct fileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen) {
  372. brelse(epos.bh);
  373. cfi->descTag.tagSerialNum = cpu_to_le16(1);
  374. cfi->fileVersionNum = cpu_to_le16(1);
  375. cfi->fileCharacteristics = 0;
  376. cfi->lengthFileIdent = namelen;
  377. cfi->lengthOfImpUse = cpu_to_le16(0);
  378. if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
  379. return fi;
  380. } else {
  381. *err = -EIO;
  382. return NULL;
  383. }
  384. }
  385. }
  386. if (!lfi || !dentry)
  387. continue;
  388. if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
  389. udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name)) {
  390. if (fibh->sbh != fibh->ebh)
  391. brelse(fibh->ebh);
  392. brelse(fibh->sbh);
  393. brelse(epos.bh);
  394. *err = -EEXIST;
  395. return NULL;
  396. }
  397. }
  398. add:
  399. f_pos += nfidlen;
  400. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
  401. sb->s_blocksize - fibh->eoffset < nfidlen) {
  402. brelse(epos.bh);
  403. epos.bh = NULL;
  404. fibh->soffset -= udf_ext0_offset(dir);
  405. fibh->eoffset -= udf_ext0_offset(dir);
  406. f_pos -= (udf_ext0_offset(dir) >> 2);
  407. if (fibh->sbh != fibh->ebh)
  408. brelse(fibh->ebh);
  409. brelse(fibh->sbh);
  410. if (!(fibh->sbh = fibh->ebh = udf_expand_dir_adinicb(dir, &block, err)))
  411. return NULL;
  412. epos.block = UDF_I_LOCATION(dir);
  413. eloc.logicalBlockNum = block;
  414. eloc.partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
  415. elen = dir->i_sb->s_blocksize;
  416. epos.offset = udf_file_entry_alloc_offset(dir);
  417. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
  418. epos.offset += sizeof(short_ad);
  419. else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
  420. epos.offset += sizeof(long_ad);
  421. }
  422. if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
  423. fibh->soffset = fibh->eoffset;
  424. fibh->eoffset += nfidlen;
  425. if (fibh->sbh != fibh->ebh) {
  426. brelse(fibh->sbh);
  427. fibh->sbh = fibh->ebh;
  428. }
  429. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
  430. block = UDF_I_LOCATION(dir).logicalBlockNum;
  431. fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) + fibh->soffset -
  432. udf_ext0_offset(dir) +
  433. UDF_I_LENEATTR(dir));
  434. } else {
  435. block = eloc.logicalBlockNum + ((elen - 1) >>
  436. dir->i_sb->s_blocksize_bits);
  437. fi = (struct fileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
  438. }
  439. } else {
  440. fibh->soffset = fibh->eoffset - sb->s_blocksize;
  441. fibh->eoffset += nfidlen - sb->s_blocksize;
  442. if (fibh->sbh != fibh->ebh) {
  443. brelse(fibh->sbh);
  444. fibh->sbh = fibh->ebh;
  445. }
  446. block = eloc.logicalBlockNum + ((elen - 1) >>
  447. dir->i_sb->s_blocksize_bits);
  448. fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err);
  449. if (!fibh->ebh) {
  450. brelse(epos.bh);
  451. brelse(fibh->sbh);
  452. return NULL;
  453. }
  454. if (!fibh->soffset) {
  455. if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
  456. (EXT_RECORDED_ALLOCATED >> 30)) {
  457. block = eloc.logicalBlockNum + ((elen - 1) >>
  458. dir->i_sb->s_blocksize_bits);
  459. } else {
  460. block++;
  461. }
  462. brelse(fibh->sbh);
  463. fibh->sbh = fibh->ebh;
  464. fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
  465. } else {
  466. fi = (struct fileIdentDesc *)
  467. (fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
  468. }
  469. }
  470. memset(cfi, 0, sizeof(struct fileIdentDesc));
  471. if (UDF_SB(sb)->s_udfrev >= 0x0200)
  472. udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, sizeof(tag));
  473. else
  474. udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, sizeof(tag));
  475. cfi->fileVersionNum = cpu_to_le16(1);
  476. cfi->lengthFileIdent = namelen;
  477. cfi->lengthOfImpUse = cpu_to_le16(0);
  478. if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
  479. brelse(epos.bh);
  480. dir->i_size += nfidlen;
  481. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  482. UDF_I_LENALLOC(dir) += nfidlen;
  483. mark_inode_dirty(dir);
  484. return fi;
  485. } else {
  486. brelse(epos.bh);
  487. if (fibh->sbh != fibh->ebh)
  488. brelse(fibh->ebh);
  489. brelse(fibh->sbh);
  490. *err = -EIO;
  491. return NULL;
  492. }
  493. }
  494. static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
  495. struct udf_fileident_bh *fibh,
  496. struct fileIdentDesc *cfi)
  497. {
  498. cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
  499. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
  500. memset(&(cfi->icb), 0x00, sizeof(long_ad));
  501. return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
  502. }
  503. static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
  504. struct nameidata *nd)
  505. {
  506. struct udf_fileident_bh fibh;
  507. struct inode *inode;
  508. struct fileIdentDesc cfi, *fi;
  509. int err;
  510. lock_kernel();
  511. inode = udf_new_inode(dir, mode, &err);
  512. if (!inode) {
  513. unlock_kernel();
  514. return err;
  515. }
  516. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
  517. inode->i_data.a_ops = &udf_adinicb_aops;
  518. else
  519. inode->i_data.a_ops = &udf_aops;
  520. inode->i_op = &udf_file_inode_operations;
  521. inode->i_fop = &udf_file_operations;
  522. inode->i_mode = mode;
  523. mark_inode_dirty(inode);
  524. if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
  525. inode->i_nlink--;
  526. mark_inode_dirty(inode);
  527. iput(inode);
  528. unlock_kernel();
  529. return err;
  530. }
  531. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  532. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
  533. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  534. cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
  535. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  536. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
  537. mark_inode_dirty(dir);
  538. }
  539. if (fibh.sbh != fibh.ebh)
  540. brelse(fibh.ebh);
  541. brelse(fibh.sbh);
  542. unlock_kernel();
  543. d_instantiate(dentry, inode);
  544. return 0;
  545. }
  546. static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
  547. dev_t rdev)
  548. {
  549. struct inode *inode;
  550. struct udf_fileident_bh fibh;
  551. struct fileIdentDesc cfi, *fi;
  552. int err;
  553. if (!old_valid_dev(rdev))
  554. return -EINVAL;
  555. lock_kernel();
  556. err = -EIO;
  557. inode = udf_new_inode(dir, mode, &err);
  558. if (!inode)
  559. goto out;
  560. inode->i_uid = current->fsuid;
  561. init_special_inode(inode, mode, rdev);
  562. if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
  563. inode->i_nlink--;
  564. mark_inode_dirty(inode);
  565. iput(inode);
  566. unlock_kernel();
  567. return err;
  568. }
  569. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  570. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
  571. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  572. cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
  573. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  574. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
  575. mark_inode_dirty(dir);
  576. }
  577. mark_inode_dirty(inode);
  578. if (fibh.sbh != fibh.ebh)
  579. brelse(fibh.ebh);
  580. brelse(fibh.sbh);
  581. d_instantiate(dentry, inode);
  582. err = 0;
  583. out:
  584. unlock_kernel();
  585. return err;
  586. }
  587. static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  588. {
  589. struct inode *inode;
  590. struct udf_fileident_bh fibh;
  591. struct fileIdentDesc cfi, *fi;
  592. int err;
  593. lock_kernel();
  594. err = -EMLINK;
  595. if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
  596. goto out;
  597. err = -EIO;
  598. inode = udf_new_inode(dir, S_IFDIR, &err);
  599. if (!inode)
  600. goto out;
  601. inode->i_op = &udf_dir_inode_operations;
  602. inode->i_fop = &udf_dir_operations;
  603. if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err))) {
  604. inode->i_nlink--;
  605. mark_inode_dirty(inode);
  606. iput(inode);
  607. goto out;
  608. }
  609. inode->i_nlink = 2;
  610. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  611. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
  612. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  613. cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
  614. cfi.fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
  615. udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
  616. brelse(fibh.sbh);
  617. inode->i_mode = S_IFDIR | mode;
  618. if (dir->i_mode & S_ISGID)
  619. inode->i_mode |= S_ISGID;
  620. mark_inode_dirty(inode);
  621. if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
  622. inode->i_nlink = 0;
  623. mark_inode_dirty(inode);
  624. iput(inode);
  625. goto out;
  626. }
  627. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  628. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
  629. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  630. cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
  631. cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
  632. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  633. inc_nlink(dir);
  634. mark_inode_dirty(dir);
  635. d_instantiate(dentry, inode);
  636. if (fibh.sbh != fibh.ebh)
  637. brelse(fibh.ebh);
  638. brelse(fibh.sbh);
  639. err = 0;
  640. out:
  641. unlock_kernel();
  642. return err;
  643. }
  644. static int empty_dir(struct inode *dir)
  645. {
  646. struct fileIdentDesc *fi, cfi;
  647. struct udf_fileident_bh fibh;
  648. loff_t f_pos;
  649. loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
  650. int block;
  651. kernel_lb_addr eloc;
  652. uint32_t elen;
  653. sector_t offset;
  654. struct extent_position epos = {};
  655. f_pos = (udf_ext0_offset(dir) >> 2);
  656. fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
  657. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
  658. fibh.sbh = fibh.ebh = NULL;
  659. } else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
  660. &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
  661. block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
  662. if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
  663. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
  664. epos.offset -= sizeof(short_ad);
  665. else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
  666. epos.offset -= sizeof(long_ad);
  667. } else {
  668. offset = 0;
  669. }
  670. if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
  671. brelse(epos.bh);
  672. return 0;
  673. }
  674. } else {
  675. brelse(epos.bh);
  676. return 0;
  677. }
  678. while ((f_pos < size)) {
  679. fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
  680. &elen, &offset);
  681. if (!fi) {
  682. if (fibh.sbh != fibh.ebh)
  683. brelse(fibh.ebh);
  684. brelse(fibh.sbh);
  685. brelse(epos.bh);
  686. return 0;
  687. }
  688. if (cfi.lengthFileIdent &&
  689. (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
  690. if (fibh.sbh != fibh.ebh)
  691. brelse(fibh.ebh);
  692. brelse(fibh.sbh);
  693. brelse(epos.bh);
  694. return 0;
  695. }
  696. }
  697. if (fibh.sbh != fibh.ebh)
  698. brelse(fibh.ebh);
  699. brelse(fibh.sbh);
  700. brelse(epos.bh);
  701. return 1;
  702. }
  703. static int udf_rmdir(struct inode *dir, struct dentry *dentry)
  704. {
  705. int retval;
  706. struct inode *inode = dentry->d_inode;
  707. struct udf_fileident_bh fibh;
  708. struct fileIdentDesc *fi, cfi;
  709. kernel_lb_addr tloc;
  710. retval = -ENOENT;
  711. lock_kernel();
  712. fi = udf_find_entry(dir, dentry, &fibh, &cfi);
  713. if (!fi)
  714. goto out;
  715. retval = -EIO;
  716. tloc = lelb_to_cpu(cfi.icb.extLocation);
  717. if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
  718. goto end_rmdir;
  719. retval = -ENOTEMPTY;
  720. if (!empty_dir(inode))
  721. goto end_rmdir;
  722. retval = udf_delete_entry(dir, fi, &fibh, &cfi);
  723. if (retval)
  724. goto end_rmdir;
  725. if (inode->i_nlink != 2)
  726. udf_warning(inode->i_sb, "udf_rmdir",
  727. "empty directory has nlink != 2 (%d)",
  728. inode->i_nlink);
  729. clear_nlink(inode);
  730. inode->i_size = 0;
  731. inode_dec_link_count(dir);
  732. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
  733. mark_inode_dirty(dir);
  734. end_rmdir:
  735. if (fibh.sbh != fibh.ebh)
  736. brelse(fibh.ebh);
  737. brelse(fibh.sbh);
  738. out:
  739. unlock_kernel();
  740. return retval;
  741. }
  742. static int udf_unlink(struct inode *dir, struct dentry *dentry)
  743. {
  744. int retval;
  745. struct inode *inode = dentry->d_inode;
  746. struct udf_fileident_bh fibh;
  747. struct fileIdentDesc *fi;
  748. struct fileIdentDesc cfi;
  749. kernel_lb_addr tloc;
  750. retval = -ENOENT;
  751. lock_kernel();
  752. fi = udf_find_entry(dir, dentry, &fibh, &cfi);
  753. if (!fi)
  754. goto out;
  755. retval = -EIO;
  756. tloc = lelb_to_cpu(cfi.icb.extLocation);
  757. if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
  758. goto end_unlink;
  759. if (!inode->i_nlink) {
  760. udf_debug("Deleting nonexistent file (%lu), %d\n",
  761. inode->i_ino, inode->i_nlink);
  762. inode->i_nlink = 1;
  763. }
  764. retval = udf_delete_entry(dir, fi, &fibh, &cfi);
  765. if (retval)
  766. goto end_unlink;
  767. dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
  768. mark_inode_dirty(dir);
  769. inode_dec_link_count(inode);
  770. inode->i_ctime = dir->i_ctime;
  771. retval = 0;
  772. end_unlink:
  773. if (fibh.sbh != fibh.ebh)
  774. brelse(fibh.ebh);
  775. brelse(fibh.sbh);
  776. out:
  777. unlock_kernel();
  778. return retval;
  779. }
  780. static int udf_symlink(struct inode *dir, struct dentry *dentry,
  781. const char *symname)
  782. {
  783. struct inode *inode;
  784. struct pathComponent *pc;
  785. char *compstart;
  786. struct udf_fileident_bh fibh;
  787. struct extent_position epos = {};
  788. int eoffset, elen = 0;
  789. struct fileIdentDesc *fi;
  790. struct fileIdentDesc cfi;
  791. char *ea;
  792. int err;
  793. int block;
  794. char name[UDF_NAME_LEN];
  795. int namelen;
  796. struct buffer_head *bh;
  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. bh = UDF_SB(inode->i_sb)->s_lvid_bh;
  890. if (bh) {
  891. struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  892. struct logicalVolHeaderDesc *lvhd;
  893. uint64_t uniqueID;
  894. lvhd = (struct logicalVolHeaderDesc *)(lvid->logicalVolContentsUse);
  895. uniqueID = le64_to_cpu(lvhd->uniqueID);
  896. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  897. cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
  898. if (!(++uniqueID & 0x00000000FFFFFFFFUL))
  899. uniqueID += 16;
  900. lvhd->uniqueID = cpu_to_le64(uniqueID);
  901. mark_buffer_dirty(bh);
  902. }
  903. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  904. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
  905. mark_inode_dirty(dir);
  906. }
  907. if (fibh.sbh != fibh.ebh)
  908. brelse(fibh.ebh);
  909. brelse(fibh.sbh);
  910. d_instantiate(dentry, inode);
  911. err = 0;
  912. out:
  913. unlock_kernel();
  914. return err;
  915. out_no_entry:
  916. inode_dec_link_count(inode);
  917. iput(inode);
  918. goto out;
  919. }
  920. static int udf_link(struct dentry *old_dentry, struct inode *dir,
  921. struct dentry *dentry)
  922. {
  923. struct inode *inode = old_dentry->d_inode;
  924. struct udf_fileident_bh fibh;
  925. struct fileIdentDesc cfi, *fi;
  926. int err;
  927. struct buffer_head *bh;
  928. lock_kernel();
  929. if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
  930. unlock_kernel();
  931. return -EMLINK;
  932. }
  933. if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
  934. unlock_kernel();
  935. return err;
  936. }
  937. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  938. cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
  939. bh = UDF_SB(inode->i_sb)->s_lvid_bh;
  940. if (bh) {
  941. struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  942. struct logicalVolHeaderDesc *lvhd;
  943. uint64_t uniqueID;
  944. lvhd = (struct logicalVolHeaderDesc *)(lvid->logicalVolContentsUse);
  945. uniqueID = le64_to_cpu(lvhd->uniqueID);
  946. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  947. cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
  948. if (!(++uniqueID & 0x00000000FFFFFFFFUL))
  949. uniqueID += 16;
  950. lvhd->uniqueID = cpu_to_le64(uniqueID);
  951. mark_buffer_dirty(bh);
  952. }
  953. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  954. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
  955. mark_inode_dirty(dir);
  956. }
  957. if (fibh.sbh != fibh.ebh)
  958. brelse(fibh.ebh);
  959. brelse(fibh.sbh);
  960. inc_nlink(inode);
  961. inode->i_ctime = current_fs_time(inode->i_sb);
  962. mark_inode_dirty(inode);
  963. atomic_inc(&inode->i_count);
  964. d_instantiate(dentry, inode);
  965. unlock_kernel();
  966. return 0;
  967. }
  968. /* Anybody can rename anything with this: the permission checks are left to the
  969. * higher-level routines.
  970. */
  971. static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
  972. struct inode *new_dir, struct dentry *new_dentry)
  973. {
  974. struct inode *old_inode = old_dentry->d_inode;
  975. struct inode *new_inode = new_dentry->d_inode;
  976. struct udf_fileident_bh ofibh, nfibh;
  977. struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
  978. struct buffer_head *dir_bh = NULL;
  979. int retval = -ENOENT;
  980. kernel_lb_addr tloc;
  981. lock_kernel();
  982. if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi))) {
  983. if (ofibh.sbh != ofibh.ebh)
  984. brelse(ofibh.ebh);
  985. brelse(ofibh.sbh);
  986. }
  987. tloc = lelb_to_cpu(ocfi.icb.extLocation);
  988. if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
  989. != old_inode->i_ino)
  990. goto end_rename;
  991. nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
  992. if (nfi) {
  993. if (!new_inode) {
  994. if (nfibh.sbh != nfibh.ebh)
  995. brelse(nfibh.ebh);
  996. brelse(nfibh.sbh);
  997. nfi = NULL;
  998. }
  999. }
  1000. if (S_ISDIR(old_inode->i_mode)) {
  1001. uint32_t offset = udf_ext0_offset(old_inode);
  1002. if (new_inode) {
  1003. retval = -ENOTEMPTY;
  1004. if (!empty_dir(new_inode))
  1005. goto end_rename;
  1006. }
  1007. retval = -EIO;
  1008. if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
  1009. dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
  1010. (UDF_I_EFE(old_inode) ?
  1011. sizeof(struct extendedFileEntry) :
  1012. sizeof(struct fileEntry)),
  1013. old_inode->i_sb->s_blocksize, &offset);
  1014. } else {
  1015. dir_bh = udf_bread(old_inode, 0, 0, &retval);
  1016. if (!dir_bh)
  1017. goto end_rename;
  1018. dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
  1019. }
  1020. if (!dir_fi)
  1021. goto end_rename;
  1022. tloc = lelb_to_cpu(dir_fi->icb.extLocation);
  1023. if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) != old_dir->i_ino)
  1024. goto end_rename;
  1025. retval = -EMLINK;
  1026. if (!new_inode && new_dir->i_nlink >= (256 << sizeof(new_dir->i_nlink)) - 1)
  1027. goto end_rename;
  1028. }
  1029. if (!nfi) {
  1030. nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
  1031. if (!nfi)
  1032. goto end_rename;
  1033. }
  1034. /*
  1035. * Like most other Unix systems, set the ctime for inodes on a
  1036. * rename.
  1037. */
  1038. old_inode->i_ctime = current_fs_time(old_inode->i_sb);
  1039. mark_inode_dirty(old_inode);
  1040. /*
  1041. * ok, that's it
  1042. */
  1043. ncfi.fileVersionNum = ocfi.fileVersionNum;
  1044. ncfi.fileCharacteristics = ocfi.fileCharacteristics;
  1045. memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
  1046. udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
  1047. /* The old fid may have moved - find it again */
  1048. ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
  1049. udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
  1050. if (new_inode) {
  1051. new_inode->i_ctime = current_fs_time(new_inode->i_sb);
  1052. inode_dec_link_count(new_inode);
  1053. }
  1054. old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
  1055. mark_inode_dirty(old_dir);
  1056. if (dir_fi) {
  1057. dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
  1058. udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
  1059. le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
  1060. if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
  1061. mark_inode_dirty(old_inode);
  1062. } else {
  1063. mark_buffer_dirty_inode(dir_bh, old_inode);
  1064. }
  1065. inode_dec_link_count(old_dir);
  1066. if (new_inode) {
  1067. inode_dec_link_count(new_inode);
  1068. } else {
  1069. inc_nlink(new_dir);
  1070. mark_inode_dirty(new_dir);
  1071. }
  1072. }
  1073. if (ofi) {
  1074. if (ofibh.sbh != ofibh.ebh)
  1075. brelse(ofibh.ebh);
  1076. brelse(ofibh.sbh);
  1077. }
  1078. retval = 0;
  1079. end_rename:
  1080. brelse(dir_bh);
  1081. if (nfi) {
  1082. if (nfibh.sbh != nfibh.ebh)
  1083. brelse(nfibh.ebh);
  1084. brelse(nfibh.sbh);
  1085. }
  1086. unlock_kernel();
  1087. return retval;
  1088. }
  1089. const struct inode_operations udf_dir_inode_operations = {
  1090. .lookup = udf_lookup,
  1091. .create = udf_create,
  1092. .link = udf_link,
  1093. .unlink = udf_unlink,
  1094. .symlink = udf_symlink,
  1095. .mkdir = udf_mkdir,
  1096. .rmdir = udf_rmdir,
  1097. .mknod = udf_mknod,
  1098. .rename = udf_rename,
  1099. };