namei.c 34 KB

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