A defragmentation technique determines the extent to which data blocks of a file are fragmented on disks of a computer and, in response, efficiently relocates those blocks if such relocation improves the on-disk layout of the file. Each indirect block of the file is examined and the current layout of the range of pointers referencing the data blocks is determined. In addition, the number of operations needed to retrieve those data blocks from disks is calculated. A potential new layout is then estimated based on an average fullness of the file system. If the potential new layout improves the fragmentation of the current layout, then the data blocks for that range are relocated, if there is sufficient free space on disk. Otherwise, the blocks are not relocated and the current on-disk layout of the file is maintained.