namei.c 34 KB

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