namei.c 34 KB

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