inode.c 61 KB

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