namei.c 33 KB

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