namei.c 33 KB

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