namei.c 33 KB

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