namei.c 33 KB

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