namei.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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. d_instantiate_new(dentry, inode);
  544. return 0;
  545. }
  546. static int udf_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  547. bool excl)
  548. {
  549. struct inode *inode = udf_new_inode(dir, mode);
  550. if (IS_ERR(inode))
  551. return PTR_ERR(inode);
  552. if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  553. inode->i_data.a_ops = &udf_adinicb_aops;
  554. else
  555. inode->i_data.a_ops = &udf_aops;
  556. inode->i_op = &udf_file_inode_operations;
  557. inode->i_fop = &udf_file_operations;
  558. mark_inode_dirty(inode);
  559. return udf_add_nondir(dentry, inode);
  560. }
  561. static int udf_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  562. {
  563. struct inode *inode = udf_new_inode(dir, mode);
  564. if (IS_ERR(inode))
  565. return PTR_ERR(inode);
  566. if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  567. inode->i_data.a_ops = &udf_adinicb_aops;
  568. else
  569. inode->i_data.a_ops = &udf_aops;
  570. inode->i_op = &udf_file_inode_operations;
  571. inode->i_fop = &udf_file_operations;
  572. mark_inode_dirty(inode);
  573. d_tmpfile(dentry, inode);
  574. unlock_new_inode(inode);
  575. return 0;
  576. }
  577. static int udf_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  578. dev_t rdev)
  579. {
  580. struct inode *inode;
  581. if (!old_valid_dev(rdev))
  582. return -EINVAL;
  583. inode = udf_new_inode(dir, mode);
  584. if (IS_ERR(inode))
  585. return PTR_ERR(inode);
  586. init_special_inode(inode, mode, rdev);
  587. return udf_add_nondir(dentry, inode);
  588. }
  589. static int udf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  590. {
  591. struct inode *inode;
  592. struct udf_fileident_bh fibh;
  593. struct fileIdentDesc cfi, *fi;
  594. int err;
  595. struct udf_inode_info *dinfo = UDF_I(dir);
  596. struct udf_inode_info *iinfo;
  597. inode = udf_new_inode(dir, S_IFDIR | mode);
  598. if (IS_ERR(inode))
  599. return PTR_ERR(inode);
  600. iinfo = UDF_I(inode);
  601. inode->i_op = &udf_dir_inode_operations;
  602. inode->i_fop = &udf_dir_operations;
  603. fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
  604. if (!fi) {
  605. inode_dec_link_count(inode);
  606. unlock_new_inode(inode);
  607. iput(inode);
  608. goto out;
  609. }
  610. set_nlink(inode, 2);
  611. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  612. cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
  613. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  614. cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
  615. cfi.fileCharacteristics =
  616. FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
  617. udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
  618. brelse(fibh.sbh);
  619. mark_inode_dirty(inode);
  620. fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
  621. if (!fi) {
  622. clear_nlink(inode);
  623. mark_inode_dirty(inode);
  624. unlock_new_inode(inode);
  625. iput(inode);
  626. goto out;
  627. }
  628. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  629. cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
  630. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  631. cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
  632. cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
  633. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  634. inc_nlink(dir);
  635. dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
  636. mark_inode_dirty(dir);
  637. d_instantiate_new(dentry, inode);
  638. if (fibh.sbh != fibh.ebh)
  639. brelse(fibh.ebh);
  640. brelse(fibh.sbh);
  641. err = 0;
  642. out:
  643. return err;
  644. }
  645. static int empty_dir(struct inode *dir)
  646. {
  647. struct fileIdentDesc *fi, cfi;
  648. struct udf_fileident_bh fibh;
  649. loff_t f_pos;
  650. loff_t size = udf_ext0_offset(dir) + dir->i_size;
  651. int block;
  652. struct kernel_lb_addr eloc;
  653. uint32_t elen;
  654. sector_t offset;
  655. struct extent_position epos = {};
  656. struct udf_inode_info *dinfo = UDF_I(dir);
  657. f_pos = udf_ext0_offset(dir);
  658. fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
  659. if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  660. fibh.sbh = fibh.ebh = NULL;
  661. else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
  662. &epos, &eloc, &elen, &offset) ==
  663. (EXT_RECORDED_ALLOCATED >> 30)) {
  664. block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
  665. if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
  666. if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  667. epos.offset -= sizeof(struct short_ad);
  668. else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  669. epos.offset -= sizeof(struct long_ad);
  670. } else
  671. offset = 0;
  672. fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
  673. if (!fibh.sbh) {
  674. brelse(epos.bh);
  675. return 0;
  676. }
  677. } else {
  678. brelse(epos.bh);
  679. return 0;
  680. }
  681. while (f_pos < size) {
  682. fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
  683. &elen, &offset);
  684. if (!fi) {
  685. if (fibh.sbh != fibh.ebh)
  686. brelse(fibh.ebh);
  687. brelse(fibh.sbh);
  688. brelse(epos.bh);
  689. return 0;
  690. }
  691. if (cfi.lengthFileIdent &&
  692. (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
  693. if (fibh.sbh != fibh.ebh)
  694. brelse(fibh.ebh);
  695. brelse(fibh.sbh);
  696. brelse(epos.bh);
  697. return 0;
  698. }
  699. }
  700. if (fibh.sbh != fibh.ebh)
  701. brelse(fibh.ebh);
  702. brelse(fibh.sbh);
  703. brelse(epos.bh);
  704. return 1;
  705. }
  706. static int udf_rmdir(struct inode *dir, struct dentry *dentry)
  707. {
  708. int retval;
  709. struct inode *inode = d_inode(dentry);
  710. struct udf_fileident_bh fibh;
  711. struct fileIdentDesc *fi, cfi;
  712. struct kernel_lb_addr tloc;
  713. retval = -ENOENT;
  714. fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
  715. if (IS_ERR_OR_NULL(fi)) {
  716. if (fi)
  717. retval = PTR_ERR(fi);
  718. goto out;
  719. }
  720. retval = -EIO;
  721. tloc = lelb_to_cpu(cfi.icb.extLocation);
  722. if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
  723. goto end_rmdir;
  724. retval = -ENOTEMPTY;
  725. if (!empty_dir(inode))
  726. goto end_rmdir;
  727. retval = udf_delete_entry(dir, fi, &fibh, &cfi);
  728. if (retval)
  729. goto end_rmdir;
  730. if (inode->i_nlink != 2)
  731. udf_warn(inode->i_sb, "empty directory has nlink != 2 (%d)\n",
  732. inode->i_nlink);
  733. clear_nlink(inode);
  734. inode->i_size = 0;
  735. inode_dec_link_count(dir);
  736. inode->i_ctime = dir->i_ctime = dir->i_mtime =
  737. current_fs_time(dir->i_sb);
  738. mark_inode_dirty(dir);
  739. end_rmdir:
  740. if (fibh.sbh != fibh.ebh)
  741. brelse(fibh.ebh);
  742. brelse(fibh.sbh);
  743. out:
  744. return retval;
  745. }
  746. static int udf_unlink(struct inode *dir, struct dentry *dentry)
  747. {
  748. int retval;
  749. struct inode *inode = d_inode(dentry);
  750. struct udf_fileident_bh fibh;
  751. struct fileIdentDesc *fi;
  752. struct fileIdentDesc cfi;
  753. struct kernel_lb_addr tloc;
  754. retval = -ENOENT;
  755. fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
  756. if (IS_ERR_OR_NULL(fi)) {
  757. if (fi)
  758. retval = PTR_ERR(fi);
  759. goto out;
  760. }
  761. retval = -EIO;
  762. tloc = lelb_to_cpu(cfi.icb.extLocation);
  763. if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
  764. goto end_unlink;
  765. if (!inode->i_nlink) {
  766. udf_debug("Deleting nonexistent file (%lu), %d\n",
  767. inode->i_ino, inode->i_nlink);
  768. set_nlink(inode, 1);
  769. }
  770. retval = udf_delete_entry(dir, fi, &fibh, &cfi);
  771. if (retval)
  772. goto end_unlink;
  773. dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
  774. mark_inode_dirty(dir);
  775. inode_dec_link_count(inode);
  776. inode->i_ctime = dir->i_ctime;
  777. retval = 0;
  778. end_unlink:
  779. if (fibh.sbh != fibh.ebh)
  780. brelse(fibh.ebh);
  781. brelse(fibh.sbh);
  782. out:
  783. return retval;
  784. }
  785. static int udf_symlink(struct inode *dir, struct dentry *dentry,
  786. const char *symname)
  787. {
  788. struct inode *inode = udf_new_inode(dir, S_IFLNK | S_IRWXUGO);
  789. struct pathComponent *pc;
  790. const char *compstart;
  791. struct extent_position epos = {};
  792. int eoffset, elen = 0;
  793. uint8_t *ea;
  794. int err;
  795. int block;
  796. unsigned char *name = NULL;
  797. int namelen;
  798. struct udf_inode_info *iinfo;
  799. struct super_block *sb = dir->i_sb;
  800. if (IS_ERR(inode))
  801. return PTR_ERR(inode);
  802. iinfo = UDF_I(inode);
  803. down_write(&iinfo->i_data_sem);
  804. name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
  805. if (!name) {
  806. err = -ENOMEM;
  807. goto out_no_entry;
  808. }
  809. inode->i_data.a_ops = &udf_symlink_aops;
  810. inode->i_op = &udf_symlink_inode_operations;
  811. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  812. struct kernel_lb_addr eloc;
  813. uint32_t bsize;
  814. block = udf_new_block(sb, inode,
  815. iinfo->i_location.partitionReferenceNum,
  816. iinfo->i_location.logicalBlockNum, &err);
  817. if (!block)
  818. goto out_no_entry;
  819. epos.block = iinfo->i_location;
  820. epos.offset = udf_file_entry_alloc_offset(inode);
  821. epos.bh = NULL;
  822. eloc.logicalBlockNum = block;
  823. eloc.partitionReferenceNum =
  824. iinfo->i_location.partitionReferenceNum;
  825. bsize = sb->s_blocksize;
  826. iinfo->i_lenExtents = bsize;
  827. udf_add_aext(inode, &epos, &eloc, bsize, 0);
  828. brelse(epos.bh);
  829. block = udf_get_pblock(sb, block,
  830. iinfo->i_location.partitionReferenceNum,
  831. 0);
  832. epos.bh = udf_tgetblk(sb, block);
  833. lock_buffer(epos.bh);
  834. memset(epos.bh->b_data, 0x00, bsize);
  835. set_buffer_uptodate(epos.bh);
  836. unlock_buffer(epos.bh);
  837. mark_buffer_dirty_inode(epos.bh, inode);
  838. ea = epos.bh->b_data + udf_ext0_offset(inode);
  839. } else
  840. ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
  841. eoffset = sb->s_blocksize - udf_ext0_offset(inode);
  842. pc = (struct pathComponent *)ea;
  843. if (*symname == '/') {
  844. do {
  845. symname++;
  846. } while (*symname == '/');
  847. pc->componentType = 1;
  848. pc->lengthComponentIdent = 0;
  849. pc->componentFileVersionNum = 0;
  850. elen += sizeof(struct pathComponent);
  851. }
  852. err = -ENAMETOOLONG;
  853. while (*symname) {
  854. if (elen + sizeof(struct pathComponent) > eoffset)
  855. goto out_no_entry;
  856. pc = (struct pathComponent *)(ea + elen);
  857. compstart = symname;
  858. do {
  859. symname++;
  860. } while (*symname && *symname != '/');
  861. pc->componentType = 5;
  862. pc->lengthComponentIdent = 0;
  863. pc->componentFileVersionNum = 0;
  864. if (compstart[0] == '.') {
  865. if ((symname - compstart) == 1)
  866. pc->componentType = 4;
  867. else if ((symname - compstart) == 2 &&
  868. compstart[1] == '.')
  869. pc->componentType = 3;
  870. }
  871. if (pc->componentType == 5) {
  872. namelen = udf_put_filename(sb, compstart, name,
  873. symname - compstart);
  874. if (!namelen)
  875. goto out_no_entry;
  876. if (elen + sizeof(struct pathComponent) + namelen >
  877. eoffset)
  878. goto out_no_entry;
  879. else
  880. pc->lengthComponentIdent = namelen;
  881. memcpy(pc->componentIdent, name, namelen);
  882. }
  883. elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
  884. if (*symname) {
  885. do {
  886. symname++;
  887. } while (*symname == '/');
  888. }
  889. }
  890. brelse(epos.bh);
  891. inode->i_size = elen;
  892. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  893. iinfo->i_lenAlloc = inode->i_size;
  894. else
  895. udf_truncate_tail_extent(inode);
  896. mark_inode_dirty(inode);
  897. up_write(&iinfo->i_data_sem);
  898. err = udf_add_nondir(dentry, inode);
  899. out:
  900. kfree(name);
  901. return err;
  902. out_no_entry:
  903. up_write(&iinfo->i_data_sem);
  904. inode_dec_link_count(inode);
  905. unlock_new_inode(inode);
  906. iput(inode);
  907. goto out;
  908. }
  909. static int udf_link(struct dentry *old_dentry, struct inode *dir,
  910. struct dentry *dentry)
  911. {
  912. struct inode *inode = d_inode(old_dentry);
  913. struct udf_fileident_bh fibh;
  914. struct fileIdentDesc cfi, *fi;
  915. int err;
  916. fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
  917. if (!fi) {
  918. return err;
  919. }
  920. cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
  921. cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
  922. if (UDF_SB(inode->i_sb)->s_lvid_bh) {
  923. *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
  924. cpu_to_le32(lvid_get_unique_id(inode->i_sb));
  925. }
  926. udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
  927. if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  928. mark_inode_dirty(dir);
  929. if (fibh.sbh != fibh.ebh)
  930. brelse(fibh.ebh);
  931. brelse(fibh.sbh);
  932. inc_nlink(inode);
  933. inode->i_ctime = current_fs_time(inode->i_sb);
  934. mark_inode_dirty(inode);
  935. dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
  936. mark_inode_dirty(dir);
  937. ihold(inode);
  938. d_instantiate(dentry, inode);
  939. return 0;
  940. }
  941. /* Anybody can rename anything with this: the permission checks are left to the
  942. * higher-level routines.
  943. */
  944. static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
  945. struct inode *new_dir, struct dentry *new_dentry)
  946. {
  947. struct inode *old_inode = d_inode(old_dentry);
  948. struct inode *new_inode = d_inode(new_dentry);
  949. struct udf_fileident_bh ofibh, nfibh;
  950. struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
  951. struct fileIdentDesc ocfi, ncfi;
  952. struct buffer_head *dir_bh = NULL;
  953. int retval = -ENOENT;
  954. struct kernel_lb_addr tloc;
  955. struct udf_inode_info *old_iinfo = UDF_I(old_inode);
  956. ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
  957. if (IS_ERR(ofi)) {
  958. retval = PTR_ERR(ofi);
  959. goto end_rename;
  960. }
  961. if (ofibh.sbh != ofibh.ebh)
  962. brelse(ofibh.ebh);
  963. brelse(ofibh.sbh);
  964. tloc = lelb_to_cpu(ocfi.icb.extLocation);
  965. if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
  966. != old_inode->i_ino)
  967. goto end_rename;
  968. nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
  969. if (IS_ERR(nfi)) {
  970. retval = PTR_ERR(nfi);
  971. goto end_rename;
  972. }
  973. if (nfi && !new_inode) {
  974. if (nfibh.sbh != nfibh.ebh)
  975. brelse(nfibh.ebh);
  976. brelse(nfibh.sbh);
  977. nfi = NULL;
  978. }
  979. if (S_ISDIR(old_inode->i_mode)) {
  980. int offset = udf_ext0_offset(old_inode);
  981. if (new_inode) {
  982. retval = -ENOTEMPTY;
  983. if (!empty_dir(new_inode))
  984. goto end_rename;
  985. }
  986. retval = -EIO;
  987. if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  988. dir_fi = udf_get_fileident(
  989. old_iinfo->i_ext.i_data -
  990. (old_iinfo->i_efe ?
  991. sizeof(struct extendedFileEntry) :
  992. sizeof(struct fileEntry)),
  993. old_inode->i_sb->s_blocksize, &offset);
  994. } else {
  995. dir_bh = udf_bread(old_inode, 0, 0, &retval);
  996. if (!dir_bh)
  997. goto end_rename;
  998. dir_fi = udf_get_fileident(dir_bh->b_data,
  999. old_inode->i_sb->s_blocksize, &offset);
  1000. }
  1001. if (!dir_fi)
  1002. goto end_rename;
  1003. tloc = lelb_to_cpu(dir_fi->icb.extLocation);
  1004. if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
  1005. old_dir->i_ino)
  1006. goto end_rename;
  1007. }
  1008. if (!nfi) {
  1009. nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
  1010. &retval);
  1011. if (!nfi)
  1012. goto end_rename;
  1013. }
  1014. /*
  1015. * Like most other Unix systems, set the ctime for inodes on a
  1016. * rename.
  1017. */
  1018. old_inode->i_ctime = current_fs_time(old_inode->i_sb);
  1019. mark_inode_dirty(old_inode);
  1020. /*
  1021. * ok, that's it
  1022. */
  1023. ncfi.fileVersionNum = ocfi.fileVersionNum;
  1024. ncfi.fileCharacteristics = ocfi.fileCharacteristics;
  1025. memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(struct long_ad));
  1026. udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
  1027. /* The old fid may have moved - find it again */
  1028. ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
  1029. udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
  1030. if (new_inode) {
  1031. new_inode->i_ctime = current_fs_time(new_inode->i_sb);
  1032. inode_dec_link_count(new_inode);
  1033. }
  1034. old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
  1035. new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
  1036. mark_inode_dirty(old_dir);
  1037. mark_inode_dirty(new_dir);
  1038. if (dir_fi) {
  1039. dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
  1040. udf_update_tag((char *)dir_fi,
  1041. (sizeof(struct fileIdentDesc) +
  1042. le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
  1043. if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1044. mark_inode_dirty(old_inode);
  1045. else
  1046. mark_buffer_dirty_inode(dir_bh, old_inode);
  1047. inode_dec_link_count(old_dir);
  1048. if (new_inode)
  1049. inode_dec_link_count(new_inode);
  1050. else {
  1051. inc_nlink(new_dir);
  1052. mark_inode_dirty(new_dir);
  1053. }
  1054. }
  1055. if (ofi) {
  1056. if (ofibh.sbh != ofibh.ebh)
  1057. brelse(ofibh.ebh);
  1058. brelse(ofibh.sbh);
  1059. }
  1060. retval = 0;
  1061. end_rename:
  1062. brelse(dir_bh);
  1063. if (nfi) {
  1064. if (nfibh.sbh != nfibh.ebh)
  1065. brelse(nfibh.ebh);
  1066. brelse(nfibh.sbh);
  1067. }
  1068. return retval;
  1069. }
  1070. static struct dentry *udf_get_parent(struct dentry *child)
  1071. {
  1072. struct kernel_lb_addr tloc;
  1073. struct inode *inode = NULL;
  1074. struct qstr dotdot = QSTR_INIT("..", 2);
  1075. struct fileIdentDesc cfi;
  1076. struct udf_fileident_bh fibh;
  1077. if (!udf_find_entry(d_inode(child), &dotdot, &fibh, &cfi))
  1078. return ERR_PTR(-EACCES);
  1079. if (fibh.sbh != fibh.ebh)
  1080. brelse(fibh.ebh);
  1081. brelse(fibh.sbh);
  1082. tloc = lelb_to_cpu(cfi.icb.extLocation);
  1083. inode = udf_iget(d_inode(child)->i_sb, &tloc);
  1084. if (IS_ERR(inode))
  1085. return ERR_CAST(inode);
  1086. return d_obtain_alias(inode);
  1087. }
  1088. static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
  1089. u16 partref, __u32 generation)
  1090. {
  1091. struct inode *inode;
  1092. struct kernel_lb_addr loc;
  1093. if (block == 0)
  1094. return ERR_PTR(-ESTALE);
  1095. loc.logicalBlockNum = block;
  1096. loc.partitionReferenceNum = partref;
  1097. inode = udf_iget(sb, &loc);
  1098. if (IS_ERR(inode))
  1099. return ERR_CAST(inode);
  1100. if (generation && inode->i_generation != generation) {
  1101. iput(inode);
  1102. return ERR_PTR(-ESTALE);
  1103. }
  1104. return d_obtain_alias(inode);
  1105. }
  1106. static struct dentry *udf_fh_to_dentry(struct super_block *sb,
  1107. struct fid *fid, int fh_len, int fh_type)
  1108. {
  1109. if (fh_len < 3 ||
  1110. (fh_type != FILEID_UDF_WITH_PARENT &&
  1111. fh_type != FILEID_UDF_WITHOUT_PARENT))
  1112. return NULL;
  1113. return udf_nfs_get_inode(sb, fid->udf.block, fid->udf.partref,
  1114. fid->udf.generation);
  1115. }
  1116. static struct dentry *udf_fh_to_parent(struct super_block *sb,
  1117. struct fid *fid, int fh_len, int fh_type)
  1118. {
  1119. if (fh_len < 5 || fh_type != FILEID_UDF_WITH_PARENT)
  1120. return NULL;
  1121. return udf_nfs_get_inode(sb, fid->udf.parent_block,
  1122. fid->udf.parent_partref,
  1123. fid->udf.parent_generation);
  1124. }
  1125. static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
  1126. struct inode *parent)
  1127. {
  1128. int len = *lenp;
  1129. struct kernel_lb_addr location = UDF_I(inode)->i_location;
  1130. struct fid *fid = (struct fid *)fh;
  1131. int type = FILEID_UDF_WITHOUT_PARENT;
  1132. if (parent && (len < 5)) {
  1133. *lenp = 5;
  1134. return FILEID_INVALID;
  1135. } else if (len < 3) {
  1136. *lenp = 3;
  1137. return FILEID_INVALID;
  1138. }
  1139. *lenp = 3;
  1140. fid->udf.block = location.logicalBlockNum;
  1141. fid->udf.partref = location.partitionReferenceNum;
  1142. fid->udf.parent_partref = 0;
  1143. fid->udf.generation = inode->i_generation;
  1144. if (parent) {
  1145. location = UDF_I(parent)->i_location;
  1146. fid->udf.parent_block = location.logicalBlockNum;
  1147. fid->udf.parent_partref = location.partitionReferenceNum;
  1148. fid->udf.parent_generation = inode->i_generation;
  1149. *lenp = 5;
  1150. type = FILEID_UDF_WITH_PARENT;
  1151. }
  1152. return type;
  1153. }
  1154. const struct export_operations udf_export_ops = {
  1155. .encode_fh = udf_encode_fh,
  1156. .fh_to_dentry = udf_fh_to_dentry,
  1157. .fh_to_parent = udf_fh_to_parent,
  1158. .get_parent = udf_get_parent,
  1159. };
  1160. const struct inode_operations udf_dir_inode_operations = {
  1161. .lookup = udf_lookup,
  1162. .create = udf_create,
  1163. .link = udf_link,
  1164. .unlink = udf_unlink,
  1165. .symlink = udf_symlink,
  1166. .mkdir = udf_mkdir,
  1167. .rmdir = udf_rmdir,
  1168. .mknod = udf_mknod,
  1169. .rename = udf_rename,
  1170. .tmpfile = udf_tmpfile,
  1171. };
  1172. const struct inode_operations udf_symlink_inode_operations = {
  1173. .readlink = generic_readlink,
  1174. .follow_link = page_follow_link_light,
  1175. .put_link = page_put_link,
  1176. };