namei.c 33 KB

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