inode.c 65 KB

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