namei.c 33 KB

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