namei.c 34 KB

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