inode.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  1. /*
  2. * inode.c
  3. *
  4. * PURPOSE
  5. * Inode 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 Dave Boynton
  14. * (C) 1998-2004 Ben Fennema
  15. * (C) 1999-2000 Stelias Computing Inc
  16. *
  17. * HISTORY
  18. *
  19. * 10/04/98 dgb Added rudimentary directory functions
  20. * 10/07/98 Fully working udf_block_map! It works!
  21. * 11/25/98 bmap altered to better support extents
  22. * 12/06/98 blf partition support in udf_iget, udf_block_map
  23. * and udf_read_inode
  24. * 12/12/98 rewrote udf_block_map to handle next extents and descs across
  25. * block boundaries (which is not actually allowed)
  26. * 12/20/98 added support for strategy 4096
  27. * 03/07/99 rewrote udf_block_map (again)
  28. * New funcs, inode_bmap, udf_next_aext
  29. * 04/19/99 Support for writing device EA's for major/minor #
  30. */
  31. #include "udfdecl.h"
  32. #include <linux/mm.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/module.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/writeback.h>
  38. #include <linux/slab.h>
  39. #include <linux/crc-itu-t.h>
  40. #include "udf_i.h"
  41. #include "udf_sb.h"
  42. MODULE_AUTHOR("Ben Fennema");
  43. MODULE_DESCRIPTION("Universal Disk Format Filesystem");
  44. MODULE_LICENSE("GPL");
  45. #define EXTENT_MERGE_SIZE 5
  46. static mode_t udf_convert_permissions(struct fileEntry *);
  47. static int udf_update_inode(struct inode *, int);
  48. static void udf_fill_inode(struct inode *, struct buffer_head *);
  49. static int udf_alloc_i_data(struct inode *inode, size_t size);
  50. static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
  51. sector_t *, int *);
  52. static int8_t udf_insert_aext(struct inode *, struct extent_position,
  53. struct kernel_lb_addr, uint32_t);
  54. static void udf_split_extents(struct inode *, int *, int, int,
  55. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  56. static void udf_prealloc_extents(struct inode *, int, int,
  57. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  58. static void udf_merge_extents(struct inode *,
  59. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  60. static void udf_update_extents(struct inode *,
  61. struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
  62. struct extent_position *);
  63. static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
  64. void udf_delete_inode(struct inode *inode)
  65. {
  66. truncate_inode_pages(&inode->i_data, 0);
  67. if (is_bad_inode(inode))
  68. goto no_delete;
  69. inode->i_size = 0;
  70. udf_truncate(inode);
  71. lock_kernel();
  72. udf_update_inode(inode, IS_SYNC(inode));
  73. udf_free_inode(inode);
  74. unlock_kernel();
  75. return;
  76. no_delete:
  77. clear_inode(inode);
  78. }
  79. /*
  80. * If we are going to release inode from memory, we truncate last inode extent
  81. * to proper length. We could use drop_inode() but it's called under inode_lock
  82. * and thus we cannot mark inode dirty there. We use clear_inode() but we have
  83. * to make sure to write inode as it's not written automatically.
  84. */
  85. void udf_clear_inode(struct inode *inode)
  86. {
  87. struct udf_inode_info *iinfo = UDF_I(inode);
  88. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
  89. inode->i_size != iinfo->i_lenExtents) {
  90. printk(KERN_WARNING "UDF-fs (%s): Inode %lu (mode %o) has "
  91. "inode size %llu different from extent length %llu. "
  92. "Filesystem need not be standards compliant.\n",
  93. inode->i_sb->s_id, inode->i_ino, inode->i_mode,
  94. (unsigned long long)inode->i_size,
  95. (unsigned long long)iinfo->i_lenExtents);
  96. }
  97. kfree(iinfo->i_ext.i_data);
  98. iinfo->i_ext.i_data = NULL;
  99. }
  100. static int udf_writepage(struct page *page, struct writeback_control *wbc)
  101. {
  102. return block_write_full_page(page, udf_get_block, wbc);
  103. }
  104. static int udf_readpage(struct file *file, struct page *page)
  105. {
  106. return block_read_full_page(page, udf_get_block);
  107. }
  108. static int udf_write_begin(struct file *file, struct address_space *mapping,
  109. loff_t pos, unsigned len, unsigned flags,
  110. struct page **pagep, void **fsdata)
  111. {
  112. *pagep = NULL;
  113. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  114. udf_get_block);
  115. }
  116. static sector_t udf_bmap(struct address_space *mapping, sector_t block)
  117. {
  118. return generic_block_bmap(mapping, block, udf_get_block);
  119. }
  120. const struct address_space_operations udf_aops = {
  121. .readpage = udf_readpage,
  122. .writepage = udf_writepage,
  123. .sync_page = block_sync_page,
  124. .write_begin = udf_write_begin,
  125. .write_end = generic_write_end,
  126. .bmap = udf_bmap,
  127. };
  128. void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
  129. {
  130. struct page *page;
  131. char *kaddr;
  132. struct udf_inode_info *iinfo = UDF_I(inode);
  133. struct writeback_control udf_wbc = {
  134. .sync_mode = WB_SYNC_NONE,
  135. .nr_to_write = 1,
  136. };
  137. /* from now on we have normal address_space methods */
  138. inode->i_data.a_ops = &udf_aops;
  139. if (!iinfo->i_lenAlloc) {
  140. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  141. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  142. else
  143. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  144. mark_inode_dirty(inode);
  145. return;
  146. }
  147. page = grab_cache_page(inode->i_mapping, 0);
  148. BUG_ON(!PageLocked(page));
  149. if (!PageUptodate(page)) {
  150. kaddr = kmap(page);
  151. memset(kaddr + iinfo->i_lenAlloc, 0x00,
  152. PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
  153. memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
  154. iinfo->i_lenAlloc);
  155. flush_dcache_page(page);
  156. SetPageUptodate(page);
  157. kunmap(page);
  158. }
  159. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
  160. iinfo->i_lenAlloc);
  161. iinfo->i_lenAlloc = 0;
  162. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  163. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  164. else
  165. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  166. inode->i_data.a_ops->writepage(page, &udf_wbc);
  167. page_cache_release(page);
  168. mark_inode_dirty(inode);
  169. }
  170. struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
  171. int *err)
  172. {
  173. int newblock;
  174. struct buffer_head *dbh = NULL;
  175. struct kernel_lb_addr eloc;
  176. uint8_t alloctype;
  177. struct extent_position epos;
  178. struct udf_fileident_bh sfibh, dfibh;
  179. loff_t f_pos = udf_ext0_offset(inode);
  180. int size = udf_ext0_offset(inode) + inode->i_size;
  181. struct fileIdentDesc cfi, *sfi, *dfi;
  182. struct udf_inode_info *iinfo = UDF_I(inode);
  183. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  184. alloctype = ICBTAG_FLAG_AD_SHORT;
  185. else
  186. alloctype = ICBTAG_FLAG_AD_LONG;
  187. if (!inode->i_size) {
  188. iinfo->i_alloc_type = alloctype;
  189. mark_inode_dirty(inode);
  190. return NULL;
  191. }
  192. /* alloc block, and copy data to it */
  193. *block = udf_new_block(inode->i_sb, inode,
  194. iinfo->i_location.partitionReferenceNum,
  195. iinfo->i_location.logicalBlockNum, err);
  196. if (!(*block))
  197. return NULL;
  198. newblock = udf_get_pblock(inode->i_sb, *block,
  199. iinfo->i_location.partitionReferenceNum,
  200. 0);
  201. if (!newblock)
  202. return NULL;
  203. dbh = udf_tgetblk(inode->i_sb, newblock);
  204. if (!dbh)
  205. return NULL;
  206. lock_buffer(dbh);
  207. memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
  208. set_buffer_uptodate(dbh);
  209. unlock_buffer(dbh);
  210. mark_buffer_dirty_inode(dbh, inode);
  211. sfibh.soffset = sfibh.eoffset =
  212. f_pos & (inode->i_sb->s_blocksize - 1);
  213. sfibh.sbh = sfibh.ebh = NULL;
  214. dfibh.soffset = dfibh.eoffset = 0;
  215. dfibh.sbh = dfibh.ebh = dbh;
  216. while (f_pos < size) {
  217. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  218. sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
  219. NULL, NULL, NULL);
  220. if (!sfi) {
  221. brelse(dbh);
  222. return NULL;
  223. }
  224. iinfo->i_alloc_type = alloctype;
  225. sfi->descTag.tagLocation = cpu_to_le32(*block);
  226. dfibh.soffset = dfibh.eoffset;
  227. dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
  228. dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
  229. if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
  230. sfi->fileIdent +
  231. le16_to_cpu(sfi->lengthOfImpUse))) {
  232. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  233. brelse(dbh);
  234. return NULL;
  235. }
  236. }
  237. mark_buffer_dirty_inode(dbh, inode);
  238. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
  239. iinfo->i_lenAlloc);
  240. iinfo->i_lenAlloc = 0;
  241. eloc.logicalBlockNum = *block;
  242. eloc.partitionReferenceNum =
  243. iinfo->i_location.partitionReferenceNum;
  244. iinfo->i_lenExtents = inode->i_size;
  245. epos.bh = NULL;
  246. epos.block = iinfo->i_location;
  247. epos.offset = udf_file_entry_alloc_offset(inode);
  248. udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
  249. /* UniqueID stuff */
  250. brelse(epos.bh);
  251. mark_inode_dirty(inode);
  252. return dbh;
  253. }
  254. static int udf_get_block(struct inode *inode, sector_t block,
  255. struct buffer_head *bh_result, int create)
  256. {
  257. int err, new;
  258. struct buffer_head *bh;
  259. sector_t phys = 0;
  260. struct udf_inode_info *iinfo;
  261. if (!create) {
  262. phys = udf_block_map(inode, block);
  263. if (phys)
  264. map_bh(bh_result, inode->i_sb, phys);
  265. return 0;
  266. }
  267. err = -EIO;
  268. new = 0;
  269. bh = NULL;
  270. lock_kernel();
  271. iinfo = UDF_I(inode);
  272. if (block == iinfo->i_next_alloc_block + 1) {
  273. iinfo->i_next_alloc_block++;
  274. iinfo->i_next_alloc_goal++;
  275. }
  276. err = 0;
  277. bh = inode_getblk(inode, block, &err, &phys, &new);
  278. BUG_ON(bh);
  279. if (err)
  280. goto abort;
  281. BUG_ON(!phys);
  282. if (new)
  283. set_buffer_new(bh_result);
  284. map_bh(bh_result, inode->i_sb, phys);
  285. abort:
  286. unlock_kernel();
  287. return err;
  288. }
  289. static struct buffer_head *udf_getblk(struct inode *inode, long block,
  290. int create, int *err)
  291. {
  292. struct buffer_head *bh;
  293. struct buffer_head dummy;
  294. dummy.b_state = 0;
  295. dummy.b_blocknr = -1000;
  296. *err = udf_get_block(inode, block, &dummy, create);
  297. if (!*err && buffer_mapped(&dummy)) {
  298. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  299. if (buffer_new(&dummy)) {
  300. lock_buffer(bh);
  301. memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
  302. set_buffer_uptodate(bh);
  303. unlock_buffer(bh);
  304. mark_buffer_dirty_inode(bh, inode);
  305. }
  306. return bh;
  307. }
  308. return NULL;
  309. }
  310. /* Extend the file by 'blocks' blocks, return the number of extents added */
  311. int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
  312. struct kernel_long_ad *last_ext, sector_t blocks)
  313. {
  314. sector_t add;
  315. int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  316. struct super_block *sb = inode->i_sb;
  317. struct kernel_lb_addr prealloc_loc = {};
  318. int prealloc_len = 0;
  319. struct udf_inode_info *iinfo;
  320. /* The previous extent is fake and we should not extend by anything
  321. * - there's nothing to do... */
  322. if (!blocks && fake)
  323. return 0;
  324. iinfo = UDF_I(inode);
  325. /* Round the last extent up to a multiple of block size */
  326. if (last_ext->extLength & (sb->s_blocksize - 1)) {
  327. last_ext->extLength =
  328. (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
  329. (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
  330. sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
  331. iinfo->i_lenExtents =
  332. (iinfo->i_lenExtents + sb->s_blocksize - 1) &
  333. ~(sb->s_blocksize - 1);
  334. }
  335. /* Last extent are just preallocated blocks? */
  336. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  337. EXT_NOT_RECORDED_ALLOCATED) {
  338. /* Save the extent so that we can reattach it to the end */
  339. prealloc_loc = last_ext->extLocation;
  340. prealloc_len = last_ext->extLength;
  341. /* Mark the extent as a hole */
  342. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  343. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  344. last_ext->extLocation.logicalBlockNum = 0;
  345. last_ext->extLocation.partitionReferenceNum = 0;
  346. }
  347. /* Can we merge with the previous extent? */
  348. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  349. EXT_NOT_RECORDED_NOT_ALLOCATED) {
  350. add = ((1 << 30) - sb->s_blocksize -
  351. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
  352. sb->s_blocksize_bits;
  353. if (add > blocks)
  354. add = blocks;
  355. blocks -= add;
  356. last_ext->extLength += add << sb->s_blocksize_bits;
  357. }
  358. if (fake) {
  359. udf_add_aext(inode, last_pos, &last_ext->extLocation,
  360. last_ext->extLength, 1);
  361. count++;
  362. } else
  363. udf_write_aext(inode, last_pos, &last_ext->extLocation,
  364. last_ext->extLength, 1);
  365. /* Managed to do everything necessary? */
  366. if (!blocks)
  367. goto out;
  368. /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
  369. last_ext->extLocation.logicalBlockNum = 0;
  370. last_ext->extLocation.partitionReferenceNum = 0;
  371. add = (1 << (30-sb->s_blocksize_bits)) - 1;
  372. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  373. (add << sb->s_blocksize_bits);
  374. /* Create enough extents to cover the whole hole */
  375. while (blocks > add) {
  376. blocks -= add;
  377. if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
  378. last_ext->extLength, 1) == -1)
  379. return -1;
  380. count++;
  381. }
  382. if (blocks) {
  383. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  384. (blocks << sb->s_blocksize_bits);
  385. if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
  386. last_ext->extLength, 1) == -1)
  387. return -1;
  388. count++;
  389. }
  390. out:
  391. /* Do we have some preallocated blocks saved? */
  392. if (prealloc_len) {
  393. if (udf_add_aext(inode, last_pos, &prealloc_loc,
  394. prealloc_len, 1) == -1)
  395. return -1;
  396. last_ext->extLocation = prealloc_loc;
  397. last_ext->extLength = prealloc_len;
  398. count++;
  399. }
  400. /* last_pos should point to the last written extent... */
  401. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  402. last_pos->offset -= sizeof(struct short_ad);
  403. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  404. last_pos->offset -= sizeof(struct long_ad);
  405. else
  406. return -1;
  407. return count;
  408. }
  409. static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
  410. int *err, sector_t *phys, int *new)
  411. {
  412. static sector_t last_block;
  413. struct buffer_head *result = NULL;
  414. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
  415. struct extent_position prev_epos, cur_epos, next_epos;
  416. int count = 0, startnum = 0, endnum = 0;
  417. uint32_t elen = 0, tmpelen;
  418. struct kernel_lb_addr eloc, tmpeloc;
  419. int c = 1;
  420. loff_t lbcount = 0, b_off = 0;
  421. uint32_t newblocknum, newblock;
  422. sector_t offset = 0;
  423. int8_t etype;
  424. struct udf_inode_info *iinfo = UDF_I(inode);
  425. int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
  426. int lastblock = 0;
  427. prev_epos.offset = udf_file_entry_alloc_offset(inode);
  428. prev_epos.block = iinfo->i_location;
  429. prev_epos.bh = NULL;
  430. cur_epos = next_epos = prev_epos;
  431. b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
  432. /* find the extent which contains the block we are looking for.
  433. alternate between laarr[0] and laarr[1] for locations of the
  434. current extent, and the previous extent */
  435. do {
  436. if (prev_epos.bh != cur_epos.bh) {
  437. brelse(prev_epos.bh);
  438. get_bh(cur_epos.bh);
  439. prev_epos.bh = cur_epos.bh;
  440. }
  441. if (cur_epos.bh != next_epos.bh) {
  442. brelse(cur_epos.bh);
  443. get_bh(next_epos.bh);
  444. cur_epos.bh = next_epos.bh;
  445. }
  446. lbcount += elen;
  447. prev_epos.block = cur_epos.block;
  448. cur_epos.block = next_epos.block;
  449. prev_epos.offset = cur_epos.offset;
  450. cur_epos.offset = next_epos.offset;
  451. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
  452. if (etype == -1)
  453. break;
  454. c = !c;
  455. laarr[c].extLength = (etype << 30) | elen;
  456. laarr[c].extLocation = eloc;
  457. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  458. pgoal = eloc.logicalBlockNum +
  459. ((elen + inode->i_sb->s_blocksize - 1) >>
  460. inode->i_sb->s_blocksize_bits);
  461. count++;
  462. } while (lbcount + elen <= b_off);
  463. b_off -= lbcount;
  464. offset = b_off >> inode->i_sb->s_blocksize_bits;
  465. /*
  466. * Move prev_epos and cur_epos into indirect extent if we are at
  467. * the pointer to it
  468. */
  469. udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
  470. udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
  471. /* if the extent is allocated and recorded, return the block
  472. if the extent is not a multiple of the blocksize, round up */
  473. if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
  474. if (elen & (inode->i_sb->s_blocksize - 1)) {
  475. elen = EXT_RECORDED_ALLOCATED |
  476. ((elen + inode->i_sb->s_blocksize - 1) &
  477. ~(inode->i_sb->s_blocksize - 1));
  478. etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
  479. }
  480. brelse(prev_epos.bh);
  481. brelse(cur_epos.bh);
  482. brelse(next_epos.bh);
  483. newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  484. *phys = newblock;
  485. return NULL;
  486. }
  487. last_block = block;
  488. /* Are we beyond EOF? */
  489. if (etype == -1) {
  490. int ret;
  491. if (count) {
  492. if (c)
  493. laarr[0] = laarr[1];
  494. startnum = 1;
  495. } else {
  496. /* Create a fake extent when there's not one */
  497. memset(&laarr[0].extLocation, 0x00,
  498. sizeof(struct kernel_lb_addr));
  499. laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  500. /* Will udf_extend_file() create real extent from
  501. a fake one? */
  502. startnum = (offset > 0);
  503. }
  504. /* Create extents for the hole between EOF and offset */
  505. ret = udf_extend_file(inode, &prev_epos, laarr, offset);
  506. if (ret == -1) {
  507. brelse(prev_epos.bh);
  508. brelse(cur_epos.bh);
  509. brelse(next_epos.bh);
  510. /* We don't really know the error here so we just make
  511. * something up */
  512. *err = -ENOSPC;
  513. return NULL;
  514. }
  515. c = 0;
  516. offset = 0;
  517. count += ret;
  518. /* We are not covered by a preallocated extent? */
  519. if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
  520. EXT_NOT_RECORDED_ALLOCATED) {
  521. /* Is there any real extent? - otherwise we overwrite
  522. * the fake one... */
  523. if (count)
  524. c = !c;
  525. laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  526. inode->i_sb->s_blocksize;
  527. memset(&laarr[c].extLocation, 0x00,
  528. sizeof(struct kernel_lb_addr));
  529. count++;
  530. endnum++;
  531. }
  532. endnum = c + 1;
  533. lastblock = 1;
  534. } else {
  535. endnum = startnum = ((count > 2) ? 2 : count);
  536. /* if the current extent is in position 0,
  537. swap it with the previous */
  538. if (!c && count != 1) {
  539. laarr[2] = laarr[0];
  540. laarr[0] = laarr[1];
  541. laarr[1] = laarr[2];
  542. c = 1;
  543. }
  544. /* if the current block is located in an extent,
  545. read the next extent */
  546. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
  547. if (etype != -1) {
  548. laarr[c + 1].extLength = (etype << 30) | elen;
  549. laarr[c + 1].extLocation = eloc;
  550. count++;
  551. startnum++;
  552. endnum++;
  553. } else
  554. lastblock = 1;
  555. }
  556. /* if the current extent is not recorded but allocated, get the
  557. * block in the extent corresponding to the requested block */
  558. if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  559. newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
  560. else { /* otherwise, allocate a new block */
  561. if (iinfo->i_next_alloc_block == block)
  562. goal = iinfo->i_next_alloc_goal;
  563. if (!goal) {
  564. if (!(goal = pgoal)) /* XXX: what was intended here? */
  565. goal = iinfo->i_location.logicalBlockNum + 1;
  566. }
  567. newblocknum = udf_new_block(inode->i_sb, inode,
  568. iinfo->i_location.partitionReferenceNum,
  569. goal, err);
  570. if (!newblocknum) {
  571. brelse(prev_epos.bh);
  572. *err = -ENOSPC;
  573. return NULL;
  574. }
  575. iinfo->i_lenExtents += inode->i_sb->s_blocksize;
  576. }
  577. /* if the extent the requsted block is located in contains multiple
  578. * blocks, split the extent into at most three extents. blocks prior
  579. * to requested block, requested block, and blocks after requested
  580. * block */
  581. udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
  582. #ifdef UDF_PREALLOCATE
  583. /* We preallocate blocks only for regular files. It also makes sense
  584. * for directories but there's a problem when to drop the
  585. * preallocation. We might use some delayed work for that but I feel
  586. * it's overengineering for a filesystem like UDF. */
  587. if (S_ISREG(inode->i_mode))
  588. udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
  589. #endif
  590. /* merge any continuous blocks in laarr */
  591. udf_merge_extents(inode, laarr, &endnum);
  592. /* write back the new extents, inserting new extents if the new number
  593. * of extents is greater than the old number, and deleting extents if
  594. * the new number of extents is less than the old number */
  595. udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
  596. brelse(prev_epos.bh);
  597. newblock = udf_get_pblock(inode->i_sb, newblocknum,
  598. iinfo->i_location.partitionReferenceNum, 0);
  599. if (!newblock)
  600. return NULL;
  601. *phys = newblock;
  602. *err = 0;
  603. *new = 1;
  604. iinfo->i_next_alloc_block = block;
  605. iinfo->i_next_alloc_goal = newblocknum;
  606. inode->i_ctime = current_fs_time(inode->i_sb);
  607. if (IS_SYNC(inode))
  608. udf_sync_inode(inode);
  609. else
  610. mark_inode_dirty(inode);
  611. return result;
  612. }
  613. static void udf_split_extents(struct inode *inode, int *c, int offset,
  614. int newblocknum,
  615. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  616. int *endnum)
  617. {
  618. unsigned long blocksize = inode->i_sb->s_blocksize;
  619. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  620. if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
  621. (laarr[*c].extLength >> 30) ==
  622. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  623. int curr = *c;
  624. int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
  625. blocksize - 1) >> blocksize_bits;
  626. int8_t etype = (laarr[curr].extLength >> 30);
  627. if (blen == 1)
  628. ;
  629. else if (!offset || blen == offset + 1) {
  630. laarr[curr + 2] = laarr[curr + 1];
  631. laarr[curr + 1] = laarr[curr];
  632. } else {
  633. laarr[curr + 3] = laarr[curr + 1];
  634. laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
  635. }
  636. if (offset) {
  637. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  638. udf_free_blocks(inode->i_sb, inode,
  639. &laarr[curr].extLocation,
  640. 0, offset);
  641. laarr[curr].extLength =
  642. EXT_NOT_RECORDED_NOT_ALLOCATED |
  643. (offset << blocksize_bits);
  644. laarr[curr].extLocation.logicalBlockNum = 0;
  645. laarr[curr].extLocation.
  646. partitionReferenceNum = 0;
  647. } else
  648. laarr[curr].extLength = (etype << 30) |
  649. (offset << blocksize_bits);
  650. curr++;
  651. (*c)++;
  652. (*endnum)++;
  653. }
  654. laarr[curr].extLocation.logicalBlockNum = newblocknum;
  655. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  656. laarr[curr].extLocation.partitionReferenceNum =
  657. UDF_I(inode)->i_location.partitionReferenceNum;
  658. laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
  659. blocksize;
  660. curr++;
  661. if (blen != offset + 1) {
  662. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  663. laarr[curr].extLocation.logicalBlockNum +=
  664. offset + 1;
  665. laarr[curr].extLength = (etype << 30) |
  666. ((blen - (offset + 1)) << blocksize_bits);
  667. curr++;
  668. (*endnum)++;
  669. }
  670. }
  671. }
  672. static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
  673. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  674. int *endnum)
  675. {
  676. int start, length = 0, currlength = 0, i;
  677. if (*endnum >= (c + 1)) {
  678. if (!lastblock)
  679. return;
  680. else
  681. start = c;
  682. } else {
  683. if ((laarr[c + 1].extLength >> 30) ==
  684. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  685. start = c + 1;
  686. length = currlength =
  687. (((laarr[c + 1].extLength &
  688. UDF_EXTENT_LENGTH_MASK) +
  689. inode->i_sb->s_blocksize - 1) >>
  690. inode->i_sb->s_blocksize_bits);
  691. } else
  692. start = c;
  693. }
  694. for (i = start + 1; i <= *endnum; i++) {
  695. if (i == *endnum) {
  696. if (lastblock)
  697. length += UDF_DEFAULT_PREALLOC_BLOCKS;
  698. } else if ((laarr[i].extLength >> 30) ==
  699. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  700. length += (((laarr[i].extLength &
  701. UDF_EXTENT_LENGTH_MASK) +
  702. inode->i_sb->s_blocksize - 1) >>
  703. inode->i_sb->s_blocksize_bits);
  704. } else
  705. break;
  706. }
  707. if (length) {
  708. int next = laarr[start].extLocation.logicalBlockNum +
  709. (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
  710. inode->i_sb->s_blocksize - 1) >>
  711. inode->i_sb->s_blocksize_bits);
  712. int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
  713. laarr[start].extLocation.partitionReferenceNum,
  714. next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
  715. length : UDF_DEFAULT_PREALLOC_BLOCKS) -
  716. currlength);
  717. if (numalloc) {
  718. if (start == (c + 1))
  719. laarr[start].extLength +=
  720. (numalloc <<
  721. inode->i_sb->s_blocksize_bits);
  722. else {
  723. memmove(&laarr[c + 2], &laarr[c + 1],
  724. sizeof(struct long_ad) * (*endnum - (c + 1)));
  725. (*endnum)++;
  726. laarr[c + 1].extLocation.logicalBlockNum = next;
  727. laarr[c + 1].extLocation.partitionReferenceNum =
  728. laarr[c].extLocation.
  729. partitionReferenceNum;
  730. laarr[c + 1].extLength =
  731. EXT_NOT_RECORDED_ALLOCATED |
  732. (numalloc <<
  733. inode->i_sb->s_blocksize_bits);
  734. start = c + 1;
  735. }
  736. for (i = start + 1; numalloc && i < *endnum; i++) {
  737. int elen = ((laarr[i].extLength &
  738. UDF_EXTENT_LENGTH_MASK) +
  739. inode->i_sb->s_blocksize - 1) >>
  740. inode->i_sb->s_blocksize_bits;
  741. if (elen > numalloc) {
  742. laarr[i].extLength -=
  743. (numalloc <<
  744. inode->i_sb->s_blocksize_bits);
  745. numalloc = 0;
  746. } else {
  747. numalloc -= elen;
  748. if (*endnum > (i + 1))
  749. memmove(&laarr[i],
  750. &laarr[i + 1],
  751. sizeof(struct long_ad) *
  752. (*endnum - (i + 1)));
  753. i--;
  754. (*endnum)--;
  755. }
  756. }
  757. UDF_I(inode)->i_lenExtents +=
  758. numalloc << inode->i_sb->s_blocksize_bits;
  759. }
  760. }
  761. }
  762. static void udf_merge_extents(struct inode *inode,
  763. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  764. int *endnum)
  765. {
  766. int i;
  767. unsigned long blocksize = inode->i_sb->s_blocksize;
  768. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  769. for (i = 0; i < (*endnum - 1); i++) {
  770. struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
  771. struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
  772. if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
  773. (((li->extLength >> 30) ==
  774. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
  775. ((lip1->extLocation.logicalBlockNum -
  776. li->extLocation.logicalBlockNum) ==
  777. (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  778. blocksize - 1) >> blocksize_bits)))) {
  779. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  780. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  781. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  782. lip1->extLength = (lip1->extLength -
  783. (li->extLength &
  784. UDF_EXTENT_LENGTH_MASK) +
  785. UDF_EXTENT_LENGTH_MASK) &
  786. ~(blocksize - 1);
  787. li->extLength = (li->extLength &
  788. UDF_EXTENT_FLAG_MASK) +
  789. (UDF_EXTENT_LENGTH_MASK + 1) -
  790. blocksize;
  791. lip1->extLocation.logicalBlockNum =
  792. li->extLocation.logicalBlockNum +
  793. ((li->extLength &
  794. UDF_EXTENT_LENGTH_MASK) >>
  795. blocksize_bits);
  796. } else {
  797. li->extLength = lip1->extLength +
  798. (((li->extLength &
  799. UDF_EXTENT_LENGTH_MASK) +
  800. blocksize - 1) & ~(blocksize - 1));
  801. if (*endnum > (i + 2))
  802. memmove(&laarr[i + 1], &laarr[i + 2],
  803. sizeof(struct long_ad) *
  804. (*endnum - (i + 2)));
  805. i--;
  806. (*endnum)--;
  807. }
  808. } else if (((li->extLength >> 30) ==
  809. (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
  810. ((lip1->extLength >> 30) ==
  811. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
  812. udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
  813. ((li->extLength &
  814. UDF_EXTENT_LENGTH_MASK) +
  815. blocksize - 1) >> blocksize_bits);
  816. li->extLocation.logicalBlockNum = 0;
  817. li->extLocation.partitionReferenceNum = 0;
  818. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  819. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  820. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  821. lip1->extLength = (lip1->extLength -
  822. (li->extLength &
  823. UDF_EXTENT_LENGTH_MASK) +
  824. UDF_EXTENT_LENGTH_MASK) &
  825. ~(blocksize - 1);
  826. li->extLength = (li->extLength &
  827. UDF_EXTENT_FLAG_MASK) +
  828. (UDF_EXTENT_LENGTH_MASK + 1) -
  829. blocksize;
  830. } else {
  831. li->extLength = lip1->extLength +
  832. (((li->extLength &
  833. UDF_EXTENT_LENGTH_MASK) +
  834. blocksize - 1) & ~(blocksize - 1));
  835. if (*endnum > (i + 2))
  836. memmove(&laarr[i + 1], &laarr[i + 2],
  837. sizeof(struct long_ad) *
  838. (*endnum - (i + 2)));
  839. i--;
  840. (*endnum)--;
  841. }
  842. } else if ((li->extLength >> 30) ==
  843. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  844. udf_free_blocks(inode->i_sb, inode,
  845. &li->extLocation, 0,
  846. ((li->extLength &
  847. UDF_EXTENT_LENGTH_MASK) +
  848. blocksize - 1) >> blocksize_bits);
  849. li->extLocation.logicalBlockNum = 0;
  850. li->extLocation.partitionReferenceNum = 0;
  851. li->extLength = (li->extLength &
  852. UDF_EXTENT_LENGTH_MASK) |
  853. EXT_NOT_RECORDED_NOT_ALLOCATED;
  854. }
  855. }
  856. }
  857. static void udf_update_extents(struct inode *inode,
  858. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  859. int startnum, int endnum,
  860. struct extent_position *epos)
  861. {
  862. int start = 0, i;
  863. struct kernel_lb_addr tmploc;
  864. uint32_t tmplen;
  865. if (startnum > endnum) {
  866. for (i = 0; i < (startnum - endnum); i++)
  867. udf_delete_aext(inode, *epos, laarr[i].extLocation,
  868. laarr[i].extLength);
  869. } else if (startnum < endnum) {
  870. for (i = 0; i < (endnum - startnum); i++) {
  871. udf_insert_aext(inode, *epos, laarr[i].extLocation,
  872. laarr[i].extLength);
  873. udf_next_aext(inode, epos, &laarr[i].extLocation,
  874. &laarr[i].extLength, 1);
  875. start++;
  876. }
  877. }
  878. for (i = start; i < endnum; i++) {
  879. udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
  880. udf_write_aext(inode, epos, &laarr[i].extLocation,
  881. laarr[i].extLength, 1);
  882. }
  883. }
  884. struct buffer_head *udf_bread(struct inode *inode, int block,
  885. int create, int *err)
  886. {
  887. struct buffer_head *bh = NULL;
  888. bh = udf_getblk(inode, block, create, err);
  889. if (!bh)
  890. return NULL;
  891. if (buffer_uptodate(bh))
  892. return bh;
  893. ll_rw_block(READ, 1, &bh);
  894. wait_on_buffer(bh);
  895. if (buffer_uptodate(bh))
  896. return bh;
  897. brelse(bh);
  898. *err = -EIO;
  899. return NULL;
  900. }
  901. void udf_truncate(struct inode *inode)
  902. {
  903. int offset;
  904. int err;
  905. struct udf_inode_info *iinfo;
  906. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  907. S_ISLNK(inode->i_mode)))
  908. return;
  909. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  910. return;
  911. lock_kernel();
  912. iinfo = UDF_I(inode);
  913. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  914. if (inode->i_sb->s_blocksize <
  915. (udf_file_entry_alloc_offset(inode) +
  916. inode->i_size)) {
  917. udf_expand_file_adinicb(inode, inode->i_size, &err);
  918. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  919. inode->i_size = iinfo->i_lenAlloc;
  920. unlock_kernel();
  921. return;
  922. } else
  923. udf_truncate_extents(inode);
  924. } else {
  925. offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
  926. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
  927. 0x00, inode->i_sb->s_blocksize -
  928. offset - udf_file_entry_alloc_offset(inode));
  929. iinfo->i_lenAlloc = inode->i_size;
  930. }
  931. } else {
  932. block_truncate_page(inode->i_mapping, inode->i_size,
  933. udf_get_block);
  934. udf_truncate_extents(inode);
  935. }
  936. inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
  937. if (IS_SYNC(inode))
  938. udf_sync_inode(inode);
  939. else
  940. mark_inode_dirty(inode);
  941. unlock_kernel();
  942. }
  943. static void __udf_read_inode(struct inode *inode)
  944. {
  945. struct buffer_head *bh = NULL;
  946. struct fileEntry *fe;
  947. uint16_t ident;
  948. struct udf_inode_info *iinfo = UDF_I(inode);
  949. /*
  950. * Set defaults, but the inode is still incomplete!
  951. * Note: get_new_inode() sets the following on a new inode:
  952. * i_sb = sb
  953. * i_no = ino
  954. * i_flags = sb->s_flags
  955. * i_state = 0
  956. * clean_inode(): zero fills and sets
  957. * i_count = 1
  958. * i_nlink = 1
  959. * i_op = NULL;
  960. */
  961. bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
  962. if (!bh) {
  963. printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
  964. inode->i_ino);
  965. make_bad_inode(inode);
  966. return;
  967. }
  968. if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
  969. ident != TAG_IDENT_USE) {
  970. printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
  971. "failed ident=%d\n", inode->i_ino, ident);
  972. brelse(bh);
  973. make_bad_inode(inode);
  974. return;
  975. }
  976. fe = (struct fileEntry *)bh->b_data;
  977. if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
  978. struct buffer_head *ibh;
  979. ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
  980. &ident);
  981. if (ident == TAG_IDENT_IE && ibh) {
  982. struct buffer_head *nbh = NULL;
  983. struct kernel_lb_addr loc;
  984. struct indirectEntry *ie;
  985. ie = (struct indirectEntry *)ibh->b_data;
  986. loc = lelb_to_cpu(ie->indirectICB.extLocation);
  987. if (ie->indirectICB.extLength &&
  988. (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
  989. &ident))) {
  990. if (ident == TAG_IDENT_FE ||
  991. ident == TAG_IDENT_EFE) {
  992. memcpy(&iinfo->i_location,
  993. &loc,
  994. sizeof(struct kernel_lb_addr));
  995. brelse(bh);
  996. brelse(ibh);
  997. brelse(nbh);
  998. __udf_read_inode(inode);
  999. return;
  1000. }
  1001. brelse(nbh);
  1002. }
  1003. }
  1004. brelse(ibh);
  1005. } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
  1006. printk(KERN_ERR "udf: unsupported strategy type: %d\n",
  1007. le16_to_cpu(fe->icbTag.strategyType));
  1008. brelse(bh);
  1009. make_bad_inode(inode);
  1010. return;
  1011. }
  1012. udf_fill_inode(inode, bh);
  1013. brelse(bh);
  1014. }
  1015. static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
  1016. {
  1017. struct fileEntry *fe;
  1018. struct extendedFileEntry *efe;
  1019. int offset;
  1020. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1021. struct udf_inode_info *iinfo = UDF_I(inode);
  1022. fe = (struct fileEntry *)bh->b_data;
  1023. efe = (struct extendedFileEntry *)bh->b_data;
  1024. if (fe->icbTag.strategyType == cpu_to_le16(4))
  1025. iinfo->i_strat4096 = 0;
  1026. else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
  1027. iinfo->i_strat4096 = 1;
  1028. iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
  1029. ICBTAG_FLAG_AD_MASK;
  1030. iinfo->i_unique = 0;
  1031. iinfo->i_lenEAttr = 0;
  1032. iinfo->i_lenExtents = 0;
  1033. iinfo->i_lenAlloc = 0;
  1034. iinfo->i_next_alloc_block = 0;
  1035. iinfo->i_next_alloc_goal = 0;
  1036. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
  1037. iinfo->i_efe = 1;
  1038. iinfo->i_use = 0;
  1039. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1040. sizeof(struct extendedFileEntry))) {
  1041. make_bad_inode(inode);
  1042. return;
  1043. }
  1044. memcpy(iinfo->i_ext.i_data,
  1045. bh->b_data + sizeof(struct extendedFileEntry),
  1046. inode->i_sb->s_blocksize -
  1047. sizeof(struct extendedFileEntry));
  1048. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
  1049. iinfo->i_efe = 0;
  1050. iinfo->i_use = 0;
  1051. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1052. sizeof(struct fileEntry))) {
  1053. make_bad_inode(inode);
  1054. return;
  1055. }
  1056. memcpy(iinfo->i_ext.i_data,
  1057. bh->b_data + sizeof(struct fileEntry),
  1058. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1059. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1060. iinfo->i_efe = 0;
  1061. iinfo->i_use = 1;
  1062. iinfo->i_lenAlloc = le32_to_cpu(
  1063. ((struct unallocSpaceEntry *)bh->b_data)->
  1064. lengthAllocDescs);
  1065. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1066. sizeof(struct unallocSpaceEntry))) {
  1067. make_bad_inode(inode);
  1068. return;
  1069. }
  1070. memcpy(iinfo->i_ext.i_data,
  1071. bh->b_data + sizeof(struct unallocSpaceEntry),
  1072. inode->i_sb->s_blocksize -
  1073. sizeof(struct unallocSpaceEntry));
  1074. return;
  1075. }
  1076. inode->i_uid = le32_to_cpu(fe->uid);
  1077. if (inode->i_uid == -1 ||
  1078. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
  1079. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
  1080. inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
  1081. inode->i_gid = le32_to_cpu(fe->gid);
  1082. if (inode->i_gid == -1 ||
  1083. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
  1084. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
  1085. inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
  1086. inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
  1087. if (!inode->i_nlink)
  1088. inode->i_nlink = 1;
  1089. inode->i_size = le64_to_cpu(fe->informationLength);
  1090. iinfo->i_lenExtents = inode->i_size;
  1091. if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
  1092. sbi->s_fmode != UDF_INVALID_MODE)
  1093. inode->i_mode = sbi->s_fmode;
  1094. else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
  1095. sbi->s_dmode != UDF_INVALID_MODE)
  1096. inode->i_mode = sbi->s_dmode;
  1097. else
  1098. inode->i_mode = udf_convert_permissions(fe);
  1099. inode->i_mode &= ~sbi->s_umask;
  1100. if (iinfo->i_efe == 0) {
  1101. inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
  1102. (inode->i_sb->s_blocksize_bits - 9);
  1103. if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
  1104. inode->i_atime = sbi->s_record_time;
  1105. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1106. fe->modificationTime))
  1107. inode->i_mtime = sbi->s_record_time;
  1108. if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
  1109. inode->i_ctime = sbi->s_record_time;
  1110. iinfo->i_unique = le64_to_cpu(fe->uniqueID);
  1111. iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
  1112. iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
  1113. offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
  1114. } else {
  1115. inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
  1116. (inode->i_sb->s_blocksize_bits - 9);
  1117. if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
  1118. inode->i_atime = sbi->s_record_time;
  1119. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1120. efe->modificationTime))
  1121. inode->i_mtime = sbi->s_record_time;
  1122. if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
  1123. iinfo->i_crtime = sbi->s_record_time;
  1124. if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
  1125. inode->i_ctime = sbi->s_record_time;
  1126. iinfo->i_unique = le64_to_cpu(efe->uniqueID);
  1127. iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
  1128. iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
  1129. offset = sizeof(struct extendedFileEntry) +
  1130. iinfo->i_lenEAttr;
  1131. }
  1132. switch (fe->icbTag.fileType) {
  1133. case ICBTAG_FILE_TYPE_DIRECTORY:
  1134. inode->i_op = &udf_dir_inode_operations;
  1135. inode->i_fop = &udf_dir_operations;
  1136. inode->i_mode |= S_IFDIR;
  1137. inc_nlink(inode);
  1138. break;
  1139. case ICBTAG_FILE_TYPE_REALTIME:
  1140. case ICBTAG_FILE_TYPE_REGULAR:
  1141. case ICBTAG_FILE_TYPE_UNDEF:
  1142. case ICBTAG_FILE_TYPE_VAT20:
  1143. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1144. inode->i_data.a_ops = &udf_adinicb_aops;
  1145. else
  1146. inode->i_data.a_ops = &udf_aops;
  1147. inode->i_op = &udf_file_inode_operations;
  1148. inode->i_fop = &udf_file_operations;
  1149. inode->i_mode |= S_IFREG;
  1150. break;
  1151. case ICBTAG_FILE_TYPE_BLOCK:
  1152. inode->i_mode |= S_IFBLK;
  1153. break;
  1154. case ICBTAG_FILE_TYPE_CHAR:
  1155. inode->i_mode |= S_IFCHR;
  1156. break;
  1157. case ICBTAG_FILE_TYPE_FIFO:
  1158. init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
  1159. break;
  1160. case ICBTAG_FILE_TYPE_SOCKET:
  1161. init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
  1162. break;
  1163. case ICBTAG_FILE_TYPE_SYMLINK:
  1164. inode->i_data.a_ops = &udf_symlink_aops;
  1165. inode->i_op = &udf_symlink_inode_operations;
  1166. inode->i_mode = S_IFLNK | S_IRWXUGO;
  1167. break;
  1168. case ICBTAG_FILE_TYPE_MAIN:
  1169. udf_debug("METADATA FILE-----\n");
  1170. break;
  1171. case ICBTAG_FILE_TYPE_MIRROR:
  1172. udf_debug("METADATA MIRROR FILE-----\n");
  1173. break;
  1174. case ICBTAG_FILE_TYPE_BITMAP:
  1175. udf_debug("METADATA BITMAP FILE-----\n");
  1176. break;
  1177. default:
  1178. printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
  1179. "file type=%d\n", inode->i_ino,
  1180. fe->icbTag.fileType);
  1181. make_bad_inode(inode);
  1182. return;
  1183. }
  1184. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1185. struct deviceSpec *dsea =
  1186. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1187. if (dsea) {
  1188. init_special_inode(inode, inode->i_mode,
  1189. MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
  1190. le32_to_cpu(dsea->minorDeviceIdent)));
  1191. /* Developer ID ??? */
  1192. } else
  1193. make_bad_inode(inode);
  1194. }
  1195. }
  1196. static int udf_alloc_i_data(struct inode *inode, size_t size)
  1197. {
  1198. struct udf_inode_info *iinfo = UDF_I(inode);
  1199. iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
  1200. if (!iinfo->i_ext.i_data) {
  1201. printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
  1202. "no free memory\n", inode->i_ino);
  1203. return -ENOMEM;
  1204. }
  1205. return 0;
  1206. }
  1207. static mode_t udf_convert_permissions(struct fileEntry *fe)
  1208. {
  1209. mode_t mode;
  1210. uint32_t permissions;
  1211. uint32_t flags;
  1212. permissions = le32_to_cpu(fe->permissions);
  1213. flags = le16_to_cpu(fe->icbTag.flags);
  1214. mode = ((permissions) & S_IRWXO) |
  1215. ((permissions >> 2) & S_IRWXG) |
  1216. ((permissions >> 4) & S_IRWXU) |
  1217. ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
  1218. ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
  1219. ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
  1220. return mode;
  1221. }
  1222. int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
  1223. {
  1224. int ret;
  1225. lock_kernel();
  1226. ret = udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1227. unlock_kernel();
  1228. return ret;
  1229. }
  1230. int udf_sync_inode(struct inode *inode)
  1231. {
  1232. return udf_update_inode(inode, 1);
  1233. }
  1234. static int udf_update_inode(struct inode *inode, int do_sync)
  1235. {
  1236. struct buffer_head *bh = NULL;
  1237. struct fileEntry *fe;
  1238. struct extendedFileEntry *efe;
  1239. uint32_t udfperms;
  1240. uint16_t icbflags;
  1241. uint16_t crclen;
  1242. int err = 0;
  1243. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1244. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1245. struct udf_inode_info *iinfo = UDF_I(inode);
  1246. bh = udf_tgetblk(inode->i_sb,
  1247. udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
  1248. if (!bh) {
  1249. udf_debug("getblk failure\n");
  1250. return -ENOMEM;
  1251. }
  1252. lock_buffer(bh);
  1253. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  1254. fe = (struct fileEntry *)bh->b_data;
  1255. efe = (struct extendedFileEntry *)bh->b_data;
  1256. if (iinfo->i_use) {
  1257. struct unallocSpaceEntry *use =
  1258. (struct unallocSpaceEntry *)bh->b_data;
  1259. use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1260. memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
  1261. iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
  1262. sizeof(struct unallocSpaceEntry));
  1263. use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
  1264. use->descTag.tagLocation =
  1265. cpu_to_le32(iinfo->i_location.logicalBlockNum);
  1266. crclen = sizeof(struct unallocSpaceEntry) +
  1267. iinfo->i_lenAlloc - sizeof(struct tag);
  1268. use->descTag.descCRCLength = cpu_to_le16(crclen);
  1269. use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
  1270. sizeof(struct tag),
  1271. crclen));
  1272. use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
  1273. goto out;
  1274. }
  1275. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
  1276. fe->uid = cpu_to_le32(-1);
  1277. else
  1278. fe->uid = cpu_to_le32(inode->i_uid);
  1279. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
  1280. fe->gid = cpu_to_le32(-1);
  1281. else
  1282. fe->gid = cpu_to_le32(inode->i_gid);
  1283. udfperms = ((inode->i_mode & S_IRWXO)) |
  1284. ((inode->i_mode & S_IRWXG) << 2) |
  1285. ((inode->i_mode & S_IRWXU) << 4);
  1286. udfperms |= (le32_to_cpu(fe->permissions) &
  1287. (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
  1288. FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
  1289. FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
  1290. fe->permissions = cpu_to_le32(udfperms);
  1291. if (S_ISDIR(inode->i_mode))
  1292. fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
  1293. else
  1294. fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
  1295. fe->informationLength = cpu_to_le64(inode->i_size);
  1296. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1297. struct regid *eid;
  1298. struct deviceSpec *dsea =
  1299. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1300. if (!dsea) {
  1301. dsea = (struct deviceSpec *)
  1302. udf_add_extendedattr(inode,
  1303. sizeof(struct deviceSpec) +
  1304. sizeof(struct regid), 12, 0x3);
  1305. dsea->attrType = cpu_to_le32(12);
  1306. dsea->attrSubtype = 1;
  1307. dsea->attrLength = cpu_to_le32(
  1308. sizeof(struct deviceSpec) +
  1309. sizeof(struct regid));
  1310. dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
  1311. }
  1312. eid = (struct regid *)dsea->impUse;
  1313. memset(eid, 0, sizeof(struct regid));
  1314. strcpy(eid->ident, UDF_ID_DEVELOPER);
  1315. eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
  1316. eid->identSuffix[1] = UDF_OS_ID_LINUX;
  1317. dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
  1318. dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
  1319. }
  1320. if (iinfo->i_efe == 0) {
  1321. memcpy(bh->b_data + sizeof(struct fileEntry),
  1322. iinfo->i_ext.i_data,
  1323. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1324. fe->logicalBlocksRecorded = cpu_to_le64(
  1325. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1326. (blocksize_bits - 9));
  1327. udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
  1328. udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
  1329. udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
  1330. memset(&(fe->impIdent), 0, sizeof(struct regid));
  1331. strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
  1332. fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1333. fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1334. fe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1335. fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1336. fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1337. fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
  1338. crclen = sizeof(struct fileEntry);
  1339. } else {
  1340. memcpy(bh->b_data + sizeof(struct extendedFileEntry),
  1341. iinfo->i_ext.i_data,
  1342. inode->i_sb->s_blocksize -
  1343. sizeof(struct extendedFileEntry));
  1344. efe->objectSize = cpu_to_le64(inode->i_size);
  1345. efe->logicalBlocksRecorded = cpu_to_le64(
  1346. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1347. (blocksize_bits - 9));
  1348. if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
  1349. (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
  1350. iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
  1351. iinfo->i_crtime = inode->i_atime;
  1352. if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
  1353. (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
  1354. iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
  1355. iinfo->i_crtime = inode->i_mtime;
  1356. if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
  1357. (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
  1358. iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
  1359. iinfo->i_crtime = inode->i_ctime;
  1360. udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
  1361. udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
  1362. udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
  1363. udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
  1364. memset(&(efe->impIdent), 0, sizeof(struct regid));
  1365. strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
  1366. efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1367. efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1368. efe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1369. efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1370. efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1371. efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
  1372. crclen = sizeof(struct extendedFileEntry);
  1373. }
  1374. if (iinfo->i_strat4096) {
  1375. fe->icbTag.strategyType = cpu_to_le16(4096);
  1376. fe->icbTag.strategyParameter = cpu_to_le16(1);
  1377. fe->icbTag.numEntries = cpu_to_le16(2);
  1378. } else {
  1379. fe->icbTag.strategyType = cpu_to_le16(4);
  1380. fe->icbTag.numEntries = cpu_to_le16(1);
  1381. }
  1382. if (S_ISDIR(inode->i_mode))
  1383. fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
  1384. else if (S_ISREG(inode->i_mode))
  1385. fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
  1386. else if (S_ISLNK(inode->i_mode))
  1387. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
  1388. else if (S_ISBLK(inode->i_mode))
  1389. fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
  1390. else if (S_ISCHR(inode->i_mode))
  1391. fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
  1392. else if (S_ISFIFO(inode->i_mode))
  1393. fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
  1394. else if (S_ISSOCK(inode->i_mode))
  1395. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
  1396. icbflags = iinfo->i_alloc_type |
  1397. ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
  1398. ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
  1399. ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
  1400. (le16_to_cpu(fe->icbTag.flags) &
  1401. ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
  1402. ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
  1403. fe->icbTag.flags = cpu_to_le16(icbflags);
  1404. if (sbi->s_udfrev >= 0x0200)
  1405. fe->descTag.descVersion = cpu_to_le16(3);
  1406. else
  1407. fe->descTag.descVersion = cpu_to_le16(2);
  1408. fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
  1409. fe->descTag.tagLocation = cpu_to_le32(
  1410. iinfo->i_location.logicalBlockNum);
  1411. crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
  1412. fe->descTag.descCRCLength = cpu_to_le16(crclen);
  1413. fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
  1414. crclen));
  1415. fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
  1416. out:
  1417. set_buffer_uptodate(bh);
  1418. unlock_buffer(bh);
  1419. /* write the data blocks */
  1420. mark_buffer_dirty(bh);
  1421. if (do_sync) {
  1422. sync_dirty_buffer(bh);
  1423. if (buffer_write_io_error(bh)) {
  1424. printk(KERN_WARNING "IO error syncing udf inode "
  1425. "[%s:%08lx]\n", inode->i_sb->s_id,
  1426. inode->i_ino);
  1427. err = -EIO;
  1428. }
  1429. }
  1430. brelse(bh);
  1431. return err;
  1432. }
  1433. struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
  1434. {
  1435. unsigned long block = udf_get_lb_pblock(sb, ino, 0);
  1436. struct inode *inode = iget_locked(sb, block);
  1437. if (!inode)
  1438. return NULL;
  1439. if (inode->i_state & I_NEW) {
  1440. memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
  1441. __udf_read_inode(inode);
  1442. unlock_new_inode(inode);
  1443. }
  1444. if (is_bad_inode(inode))
  1445. goto out_iput;
  1446. if (ino->logicalBlockNum >= UDF_SB(sb)->
  1447. s_partmaps[ino->partitionReferenceNum].s_partition_len) {
  1448. udf_debug("block=%d, partition=%d out of range\n",
  1449. ino->logicalBlockNum, ino->partitionReferenceNum);
  1450. make_bad_inode(inode);
  1451. goto out_iput;
  1452. }
  1453. return inode;
  1454. out_iput:
  1455. iput(inode);
  1456. return NULL;
  1457. }
  1458. int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
  1459. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1460. {
  1461. int adsize;
  1462. struct short_ad *sad = NULL;
  1463. struct long_ad *lad = NULL;
  1464. struct allocExtDesc *aed;
  1465. int8_t etype;
  1466. uint8_t *ptr;
  1467. struct udf_inode_info *iinfo = UDF_I(inode);
  1468. if (!epos->bh)
  1469. ptr = iinfo->i_ext.i_data + epos->offset -
  1470. udf_file_entry_alloc_offset(inode) +
  1471. iinfo->i_lenEAttr;
  1472. else
  1473. ptr = epos->bh->b_data + epos->offset;
  1474. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1475. adsize = sizeof(struct short_ad);
  1476. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1477. adsize = sizeof(struct long_ad);
  1478. else
  1479. return -1;
  1480. if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
  1481. unsigned char *sptr, *dptr;
  1482. struct buffer_head *nbh;
  1483. int err, loffset;
  1484. struct kernel_lb_addr obloc = epos->block;
  1485. epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
  1486. obloc.partitionReferenceNum,
  1487. obloc.logicalBlockNum, &err);
  1488. if (!epos->block.logicalBlockNum)
  1489. return -1;
  1490. nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
  1491. &epos->block,
  1492. 0));
  1493. if (!nbh)
  1494. return -1;
  1495. lock_buffer(nbh);
  1496. memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
  1497. set_buffer_uptodate(nbh);
  1498. unlock_buffer(nbh);
  1499. mark_buffer_dirty_inode(nbh, inode);
  1500. aed = (struct allocExtDesc *)(nbh->b_data);
  1501. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
  1502. aed->previousAllocExtLocation =
  1503. cpu_to_le32(obloc.logicalBlockNum);
  1504. if (epos->offset + adsize > inode->i_sb->s_blocksize) {
  1505. loffset = epos->offset;
  1506. aed->lengthAllocDescs = cpu_to_le32(adsize);
  1507. sptr = ptr - adsize;
  1508. dptr = nbh->b_data + sizeof(struct allocExtDesc);
  1509. memcpy(dptr, sptr, adsize);
  1510. epos->offset = sizeof(struct allocExtDesc) + adsize;
  1511. } else {
  1512. loffset = epos->offset + adsize;
  1513. aed->lengthAllocDescs = cpu_to_le32(0);
  1514. sptr = ptr;
  1515. epos->offset = sizeof(struct allocExtDesc);
  1516. if (epos->bh) {
  1517. aed = (struct allocExtDesc *)epos->bh->b_data;
  1518. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1519. } else {
  1520. iinfo->i_lenAlloc += adsize;
  1521. mark_inode_dirty(inode);
  1522. }
  1523. }
  1524. if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
  1525. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
  1526. epos->block.logicalBlockNum, sizeof(struct tag));
  1527. else
  1528. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
  1529. epos->block.logicalBlockNum, sizeof(struct tag));
  1530. switch (iinfo->i_alloc_type) {
  1531. case ICBTAG_FLAG_AD_SHORT:
  1532. sad = (struct short_ad *)sptr;
  1533. sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1534. inode->i_sb->s_blocksize);
  1535. sad->extPosition =
  1536. cpu_to_le32(epos->block.logicalBlockNum);
  1537. break;
  1538. case ICBTAG_FLAG_AD_LONG:
  1539. lad = (struct long_ad *)sptr;
  1540. lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1541. inode->i_sb->s_blocksize);
  1542. lad->extLocation = cpu_to_lelb(epos->block);
  1543. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1544. break;
  1545. }
  1546. if (epos->bh) {
  1547. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1548. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1549. udf_update_tag(epos->bh->b_data, loffset);
  1550. else
  1551. udf_update_tag(epos->bh->b_data,
  1552. sizeof(struct allocExtDesc));
  1553. mark_buffer_dirty_inode(epos->bh, inode);
  1554. brelse(epos->bh);
  1555. } else {
  1556. mark_inode_dirty(inode);
  1557. }
  1558. epos->bh = nbh;
  1559. }
  1560. etype = udf_write_aext(inode, epos, eloc, elen, inc);
  1561. if (!epos->bh) {
  1562. iinfo->i_lenAlloc += adsize;
  1563. mark_inode_dirty(inode);
  1564. } else {
  1565. aed = (struct allocExtDesc *)epos->bh->b_data;
  1566. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1567. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1568. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1569. udf_update_tag(epos->bh->b_data,
  1570. epos->offset + (inc ? 0 : adsize));
  1571. else
  1572. udf_update_tag(epos->bh->b_data,
  1573. sizeof(struct allocExtDesc));
  1574. mark_buffer_dirty_inode(epos->bh, inode);
  1575. }
  1576. return etype;
  1577. }
  1578. int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
  1579. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1580. {
  1581. int adsize;
  1582. uint8_t *ptr;
  1583. struct short_ad *sad;
  1584. struct long_ad *lad;
  1585. struct udf_inode_info *iinfo = UDF_I(inode);
  1586. if (!epos->bh)
  1587. ptr = iinfo->i_ext.i_data + epos->offset -
  1588. udf_file_entry_alloc_offset(inode) +
  1589. iinfo->i_lenEAttr;
  1590. else
  1591. ptr = epos->bh->b_data + epos->offset;
  1592. switch (iinfo->i_alloc_type) {
  1593. case ICBTAG_FLAG_AD_SHORT:
  1594. sad = (struct short_ad *)ptr;
  1595. sad->extLength = cpu_to_le32(elen);
  1596. sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
  1597. adsize = sizeof(struct short_ad);
  1598. break;
  1599. case ICBTAG_FLAG_AD_LONG:
  1600. lad = (struct long_ad *)ptr;
  1601. lad->extLength = cpu_to_le32(elen);
  1602. lad->extLocation = cpu_to_lelb(*eloc);
  1603. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1604. adsize = sizeof(struct long_ad);
  1605. break;
  1606. default:
  1607. return -1;
  1608. }
  1609. if (epos->bh) {
  1610. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1611. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
  1612. struct allocExtDesc *aed =
  1613. (struct allocExtDesc *)epos->bh->b_data;
  1614. udf_update_tag(epos->bh->b_data,
  1615. le32_to_cpu(aed->lengthAllocDescs) +
  1616. sizeof(struct allocExtDesc));
  1617. }
  1618. mark_buffer_dirty_inode(epos->bh, inode);
  1619. } else {
  1620. mark_inode_dirty(inode);
  1621. }
  1622. if (inc)
  1623. epos->offset += adsize;
  1624. return (elen >> 30);
  1625. }
  1626. int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
  1627. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1628. {
  1629. int8_t etype;
  1630. while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
  1631. (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  1632. int block;
  1633. epos->block = *eloc;
  1634. epos->offset = sizeof(struct allocExtDesc);
  1635. brelse(epos->bh);
  1636. block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
  1637. epos->bh = udf_tread(inode->i_sb, block);
  1638. if (!epos->bh) {
  1639. udf_debug("reading block %d failed!\n", block);
  1640. return -1;
  1641. }
  1642. }
  1643. return etype;
  1644. }
  1645. int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
  1646. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1647. {
  1648. int alen;
  1649. int8_t etype;
  1650. uint8_t *ptr;
  1651. struct short_ad *sad;
  1652. struct long_ad *lad;
  1653. struct udf_inode_info *iinfo = UDF_I(inode);
  1654. if (!epos->bh) {
  1655. if (!epos->offset)
  1656. epos->offset = udf_file_entry_alloc_offset(inode);
  1657. ptr = iinfo->i_ext.i_data + epos->offset -
  1658. udf_file_entry_alloc_offset(inode) +
  1659. iinfo->i_lenEAttr;
  1660. alen = udf_file_entry_alloc_offset(inode) +
  1661. iinfo->i_lenAlloc;
  1662. } else {
  1663. if (!epos->offset)
  1664. epos->offset = sizeof(struct allocExtDesc);
  1665. ptr = epos->bh->b_data + epos->offset;
  1666. alen = sizeof(struct allocExtDesc) +
  1667. le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
  1668. lengthAllocDescs);
  1669. }
  1670. switch (iinfo->i_alloc_type) {
  1671. case ICBTAG_FLAG_AD_SHORT:
  1672. sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
  1673. if (!sad)
  1674. return -1;
  1675. etype = le32_to_cpu(sad->extLength) >> 30;
  1676. eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
  1677. eloc->partitionReferenceNum =
  1678. iinfo->i_location.partitionReferenceNum;
  1679. *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1680. break;
  1681. case ICBTAG_FLAG_AD_LONG:
  1682. lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
  1683. if (!lad)
  1684. return -1;
  1685. etype = le32_to_cpu(lad->extLength) >> 30;
  1686. *eloc = lelb_to_cpu(lad->extLocation);
  1687. *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1688. break;
  1689. default:
  1690. udf_debug("alloc_type = %d unsupported\n",
  1691. iinfo->i_alloc_type);
  1692. return -1;
  1693. }
  1694. return etype;
  1695. }
  1696. static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
  1697. struct kernel_lb_addr neloc, uint32_t nelen)
  1698. {
  1699. struct kernel_lb_addr oeloc;
  1700. uint32_t oelen;
  1701. int8_t etype;
  1702. if (epos.bh)
  1703. get_bh(epos.bh);
  1704. while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
  1705. udf_write_aext(inode, &epos, &neloc, nelen, 1);
  1706. neloc = oeloc;
  1707. nelen = (etype << 30) | oelen;
  1708. }
  1709. udf_add_aext(inode, &epos, &neloc, nelen, 1);
  1710. brelse(epos.bh);
  1711. return (nelen >> 30);
  1712. }
  1713. int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
  1714. struct kernel_lb_addr eloc, uint32_t elen)
  1715. {
  1716. struct extent_position oepos;
  1717. int adsize;
  1718. int8_t etype;
  1719. struct allocExtDesc *aed;
  1720. struct udf_inode_info *iinfo;
  1721. if (epos.bh) {
  1722. get_bh(epos.bh);
  1723. get_bh(epos.bh);
  1724. }
  1725. iinfo = UDF_I(inode);
  1726. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1727. adsize = sizeof(struct short_ad);
  1728. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1729. adsize = sizeof(struct long_ad);
  1730. else
  1731. adsize = 0;
  1732. oepos = epos;
  1733. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
  1734. return -1;
  1735. while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  1736. udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
  1737. if (oepos.bh != epos.bh) {
  1738. oepos.block = epos.block;
  1739. brelse(oepos.bh);
  1740. get_bh(epos.bh);
  1741. oepos.bh = epos.bh;
  1742. oepos.offset = epos.offset - adsize;
  1743. }
  1744. }
  1745. memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
  1746. elen = 0;
  1747. if (epos.bh != oepos.bh) {
  1748. udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
  1749. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1750. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1751. if (!oepos.bh) {
  1752. iinfo->i_lenAlloc -= (adsize * 2);
  1753. mark_inode_dirty(inode);
  1754. } else {
  1755. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1756. le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
  1757. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1758. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1759. udf_update_tag(oepos.bh->b_data,
  1760. oepos.offset - (2 * adsize));
  1761. else
  1762. udf_update_tag(oepos.bh->b_data,
  1763. sizeof(struct allocExtDesc));
  1764. mark_buffer_dirty_inode(oepos.bh, inode);
  1765. }
  1766. } else {
  1767. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1768. if (!oepos.bh) {
  1769. iinfo->i_lenAlloc -= adsize;
  1770. mark_inode_dirty(inode);
  1771. } else {
  1772. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1773. le32_add_cpu(&aed->lengthAllocDescs, -adsize);
  1774. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1775. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1776. udf_update_tag(oepos.bh->b_data,
  1777. epos.offset - adsize);
  1778. else
  1779. udf_update_tag(oepos.bh->b_data,
  1780. sizeof(struct allocExtDesc));
  1781. mark_buffer_dirty_inode(oepos.bh, inode);
  1782. }
  1783. }
  1784. brelse(epos.bh);
  1785. brelse(oepos.bh);
  1786. return (elen >> 30);
  1787. }
  1788. int8_t inode_bmap(struct inode *inode, sector_t block,
  1789. struct extent_position *pos, struct kernel_lb_addr *eloc,
  1790. uint32_t *elen, sector_t *offset)
  1791. {
  1792. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1793. loff_t lbcount = 0, bcount =
  1794. (loff_t) block << blocksize_bits;
  1795. int8_t etype;
  1796. struct udf_inode_info *iinfo;
  1797. iinfo = UDF_I(inode);
  1798. pos->offset = 0;
  1799. pos->block = iinfo->i_location;
  1800. pos->bh = NULL;
  1801. *elen = 0;
  1802. do {
  1803. etype = udf_next_aext(inode, pos, eloc, elen, 1);
  1804. if (etype == -1) {
  1805. *offset = (bcount - lbcount) >> blocksize_bits;
  1806. iinfo->i_lenExtents = lbcount;
  1807. return -1;
  1808. }
  1809. lbcount += *elen;
  1810. } while (lbcount <= bcount);
  1811. *offset = (bcount + *elen - lbcount) >> blocksize_bits;
  1812. return etype;
  1813. }
  1814. long udf_block_map(struct inode *inode, sector_t block)
  1815. {
  1816. struct kernel_lb_addr eloc;
  1817. uint32_t elen;
  1818. sector_t offset;
  1819. struct extent_position epos = {};
  1820. int ret;
  1821. lock_kernel();
  1822. if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
  1823. (EXT_RECORDED_ALLOCATED >> 30))
  1824. ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  1825. else
  1826. ret = 0;
  1827. unlock_kernel();
  1828. brelse(epos.bh);
  1829. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
  1830. return udf_fixed_to_variable(ret);
  1831. else
  1832. return ret;
  1833. }